mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added: Add more SharedPrefernces for termux-float and use multi-process for log level
This commit is contained in:
@@ -732,7 +732,7 @@ public final class TermuxService extends Service implements TermuxTask.TermuxTas
|
||||
// Build the notification
|
||||
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
|
||||
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID, priority,
|
||||
getText(R.string.application_name), notificationText, null,
|
||||
TermuxConstants.TERMUX_APP_NAME, notificationText, null,
|
||||
contentIntent, null, NotificationUtils.NOTIFICATION_MODE_SILENT);
|
||||
if (builder == null) return null;
|
||||
|
||||
|
@@ -21,7 +21,6 @@ public class TermuxAppSharedPreferences {
|
||||
private final Context mContext;
|
||||
private final SharedPreferences mSharedPreferences;
|
||||
|
||||
|
||||
private int MIN_FONTSIZE;
|
||||
private int MAX_FONTSIZE;
|
||||
private int DEFAULT_FONTSIZE;
|
||||
@@ -129,21 +128,33 @@ public class TermuxAppSharedPreferences {
|
||||
|
||||
|
||||
|
||||
private void setFontVariables(Context context) {
|
||||
public static int[] getDefaultFontSizes(Context context) {
|
||||
float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
|
||||
|
||||
int[] sizes = new int[3];
|
||||
|
||||
// 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);
|
||||
sizes[1] = (int) (4f * dipInPixels); // min
|
||||
|
||||
// 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;
|
||||
sizes[0] = defaultFontSize; // default
|
||||
|
||||
MAX_FONTSIZE = 256;
|
||||
sizes[2] = 256; // max
|
||||
|
||||
return sizes;
|
||||
}
|
||||
|
||||
public void setFontVariables(Context context) {
|
||||
int[] sizes = getDefaultFontSizes(context);
|
||||
|
||||
DEFAULT_FONTSIZE = sizes[0];
|
||||
MIN_FONTSIZE = sizes[1];
|
||||
MAX_FONTSIZE = sizes[2];
|
||||
}
|
||||
|
||||
public int getFontSize() {
|
||||
@@ -156,7 +167,6 @@ public class TermuxAppSharedPreferences {
|
||||
}
|
||||
|
||||
public void changeFontSize(boolean increase) {
|
||||
|
||||
int fontSize = getFontSize();
|
||||
|
||||
fontSize += (increase ? 1 : -1) * 2;
|
||||
|
@@ -6,6 +6,7 @@ import android.content.SharedPreferences;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.packages.PackageUtils;
|
||||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_FLOAT_APP;
|
||||
@@ -18,13 +19,20 @@ public class TermuxFloatAppSharedPreferences {
|
||||
|
||||
private final Context mContext;
|
||||
private final SharedPreferences mSharedPreferences;
|
||||
private final SharedPreferences mMultiProcessSharedPreferences;
|
||||
|
||||
private int MIN_FONTSIZE;
|
||||
private int MAX_FONTSIZE;
|
||||
private int DEFAULT_FONTSIZE;
|
||||
|
||||
private static final String LOG_TAG = "TermuxFloatAppSharedPreferences";
|
||||
|
||||
private TermuxFloatAppSharedPreferences(@Nonnull Context context) {
|
||||
mContext = context;
|
||||
mSharedPreferences = getPrivateSharedPreferences(mContext);
|
||||
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
|
||||
|
||||
setFontVariables(context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,11 +44,11 @@ public class TermuxFloatAppSharedPreferences {
|
||||
*/
|
||||
@Nullable
|
||||
public static TermuxFloatAppSharedPreferences build(@NonNull final Context context) {
|
||||
Context termuxTaskerPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME);
|
||||
if (termuxTaskerPackageContext == null)
|
||||
Context termuxFloatPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME);
|
||||
if (termuxFloatPackageContext == null)
|
||||
return null;
|
||||
else
|
||||
return new TermuxFloatAppSharedPreferences(termuxTaskerPackageContext);
|
||||
return new TermuxFloatAppSharedPreferences(termuxFloatPackageContext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,11 +61,11 @@ public class TermuxFloatAppSharedPreferences {
|
||||
* @return Returns the {@link TermuxFloatAppSharedPreferences}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
public static TermuxFloatAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) {
|
||||
Context termuxTaskerPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME, exitAppOnError);
|
||||
if (termuxTaskerPackageContext == null)
|
||||
Context termuxFloatPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME, exitAppOnError);
|
||||
if (termuxFloatPackageContext == null)
|
||||
return null;
|
||||
else
|
||||
return new TermuxFloatAppSharedPreferences(termuxTaskerPackageContext);
|
||||
return new TermuxFloatAppSharedPreferences(termuxFloatPackageContext);
|
||||
}
|
||||
|
||||
private static SharedPreferences getPrivateSharedPreferences(Context context) {
|
||||
@@ -65,15 +73,102 @@ public class TermuxFloatAppSharedPreferences {
|
||||
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_FLOAT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getLogLevel() {
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
|
||||
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
|
||||
if (context == null) return null;
|
||||
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_FLOAT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
||||
}
|
||||
|
||||
public void setLogLevel(Context context, int logLevel) {
|
||||
|
||||
|
||||
public int getWindowX() {
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_X, 200);
|
||||
|
||||
}
|
||||
|
||||
public void setWindowX(int value) {
|
||||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_X, value, false);
|
||||
}
|
||||
|
||||
public int getWindowY() {
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_Y, 200);
|
||||
|
||||
}
|
||||
|
||||
public void setWindowY(int value) {
|
||||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_Y, value, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getWindowWidth() {
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_WIDTH, 500);
|
||||
|
||||
}
|
||||
|
||||
public void setWindowWidth(int value) {
|
||||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_WIDTH, value, false);
|
||||
}
|
||||
|
||||
public int getWindowHeight() {
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_HEIGHT, 500);
|
||||
|
||||
}
|
||||
|
||||
public void setWindowHeight(int value) {
|
||||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_HEIGHT, value, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setFontVariables(Context context) {
|
||||
int[] sizes = TermuxAppSharedPreferences.getDefaultFontSizes(context);
|
||||
|
||||
DEFAULT_FONTSIZE = sizes[0];
|
||||
MIN_FONTSIZE = sizes[1];
|
||||
MAX_FONTSIZE = sizes[2];
|
||||
}
|
||||
|
||||
public int getFontSize() {
|
||||
int fontSize = SharedPreferenceUtils.getIntStoredAsString(mSharedPreferences, TERMUX_FLOAT_APP.KEY_FONTSIZE, DEFAULT_FONTSIZE);
|
||||
return DataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
|
||||
}
|
||||
|
||||
public void setFontSize(int value) {
|
||||
SharedPreferenceUtils.setIntStoredAsString(mSharedPreferences, TERMUX_FLOAT_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 int getLogLevel(boolean readFromFile) {
|
||||
if (readFromFile)
|
||||
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
|
||||
else
|
||||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_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_FLOAT_APP.KEY_LOG_LEVEL, logLevel, false);
|
||||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
|
||||
}
|
||||
|
||||
|
||||
public boolean isTerminalViewKeyLoggingEnabled(boolean readFromFile) {
|
||||
if (readFromFile)
|
||||
return SharedPreferenceUtils.getBoolean(mMultiProcessSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_FLOAT_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
|
||||
else
|
||||
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_FLOAT_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
|
||||
}
|
||||
|
||||
public void setTerminalViewKeyLoggingEnabled(boolean value, boolean commitToFile) {
|
||||
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, value, commitToFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.termux.shared.settings.preferences;
|
||||
|
||||
/*
|
||||
* Version: v0.12.0
|
||||
* Version: v0.13.0
|
||||
*
|
||||
* Changelog
|
||||
*
|
||||
@@ -53,6 +53,11 @@ package com.termux.shared.settings.preferences;
|
||||
* - Added `TERMUX_API_APP.KEY_LOG_LEVEL`, `TERMUX_BOOT_APP.KEY_LOG_LEVEL`,
|
||||
* `TERMUX_FLOAT_APP.KEY_LOG_LEVEL`, `TERMUX_STYLING_APP.KEY_LOG_LEVEL`,
|
||||
* `TERMUX_Widget_APP.KEY_LOG_LEVEL`.
|
||||
*
|
||||
* - 0.13.0 (2021-09-02)
|
||||
* - Added following to `TERMUX_FLOAT_APP`:
|
||||
* `KEY_WINDOW_X`, `KEY_WINDOW_Y`, `KEY_WINDOW_WIDTH`, `KEY_WINDOW_HEIGHT`, `KEY_FONTSIZE`,
|
||||
* `KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED`.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -187,11 +192,42 @@ public final class TermuxPreferenceConstants {
|
||||
*/
|
||||
public static final class TERMUX_FLOAT_APP {
|
||||
|
||||
/**
|
||||
* The float window x coordinate.
|
||||
*/
|
||||
public static final String KEY_WINDOW_X = "window_x";
|
||||
|
||||
/**
|
||||
* The float window y coordinate.
|
||||
*/
|
||||
public static final String KEY_WINDOW_Y = "window_y";
|
||||
|
||||
/**
|
||||
* The float window width.
|
||||
*/
|
||||
public static final String KEY_WINDOW_WIDTH = "window_width";
|
||||
|
||||
/**
|
||||
* The float window height.
|
||||
*/
|
||||
public static final String KEY_WINDOW_HEIGHT = "window_height";
|
||||
|
||||
/**
|
||||
* Defines the key for font size of termux terminal view.
|
||||
*/
|
||||
public static final String KEY_FONTSIZE = "fontsize";
|
||||
|
||||
/**
|
||||
* Defines the key for current log level.
|
||||
*/
|
||||
public static final String KEY_LOG_LEVEL = "log_level";
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -74,8 +74,8 @@ public class TermuxTaskerAppSharedPreferences {
|
||||
|
||||
|
||||
|
||||
public int getLogLevel(boolean readFromFfile) {
|
||||
if (readFromFfile)
|
||||
public int getLogLevel(boolean readFromFile) {
|
||||
if (readFromFile)
|
||||
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);
|
||||
|
@@ -173,7 +173,9 @@ public class ViewUtils {
|
||||
/**
|
||||
* Get device display size.
|
||||
*
|
||||
* @param context The {@link Context} to check with.
|
||||
* @param context The {@link Context} to check with. It must be {@link Activity} context, otherwise
|
||||
* android will throw:
|
||||
* `java.lang.IllegalArgumentException: Used non-visual Context to obtain an instance of WindowManager. Please use an Activity or a ContextWrapper around one instead.`
|
||||
* @param activitySize The set to {@link true}, then size returned will be that of the activity
|
||||
* and can be smaller than physical display size in multi-window mode.
|
||||
* @return Returns the display size as {@link Point}.
|
||||
|
Reference in New Issue
Block a user