Skip to content

Commit 36557b2

Browse files
Added: Add more SharedPrefernces for termux-float and use multi-process for log level
1 parent 1cf1e61 commit 36557b2

File tree

6 files changed

+164
-21
lines changed

6 files changed

+164
-21
lines changed

app/src/main/java/com/termux/app/TermuxService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ private Notification buildNotification() {
732732
// Build the notification
733733
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
734734
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID, priority,
735-
getText(R.string.application_name), notificationText, null,
735+
TermuxConstants.TERMUX_APP_NAME, notificationText, null,
736736
contentIntent, null, NotificationUtils.NOTIFICATION_MODE_SILENT);
737737
if (builder == null) return null;
738738

termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxAppSharedPreferences.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class TermuxAppSharedPreferences {
2121
private final Context mContext;
2222
private final SharedPreferences mSharedPreferences;
2323

24-
2524
private int MIN_FONTSIZE;
2625
private int MAX_FONTSIZE;
2726
private int DEFAULT_FONTSIZE;
@@ -129,21 +128,33 @@ public void setKeepScreenOn(boolean value) {
129128

130129

131130

132-
private void setFontVariables(Context context) {
131+
public static int[] getDefaultFontSizes(Context context) {
133132
float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
134133

134+
int[] sizes = new int[3];
135+
135136
// This is a bit arbitrary and sub-optimal. We want to give a sensible default for minimum font size
136137
// to prevent invisible text due to zoom be mistake:
137-
MIN_FONTSIZE = (int) (4f * dipInPixels);
138+
sizes[1] = (int) (4f * dipInPixels); // min
138139

139140
// http://www.google.com/design/spec/style/typography.html#typography-line-height
140141
int defaultFontSize = Math.round(12 * dipInPixels);
141142
// Make it divisible by 2 since that is the minimal adjustment step:
142143
if (defaultFontSize % 2 == 1) defaultFontSize--;
143144

144-
DEFAULT_FONTSIZE = defaultFontSize;
145+
sizes[0] = defaultFontSize; // default
145146

146-
MAX_FONTSIZE = 256;
147+
sizes[2] = 256; // max
148+
149+
return sizes;
150+
}
151+
152+
public void setFontVariables(Context context) {
153+
int[] sizes = getDefaultFontSizes(context);
154+
155+
DEFAULT_FONTSIZE = sizes[0];
156+
MIN_FONTSIZE = sizes[1];
157+
MAX_FONTSIZE = sizes[2];
147158
}
148159

149160
public int getFontSize() {
@@ -156,7 +167,6 @@ public void setFontSize(int value) {
156167
}
157168

158169
public void changeFontSize(boolean increase) {
159-
160170
int fontSize = getFontSize();
161171

162172
fontSize += (increase ? 1 : -1) * 2;

termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxFloatAppSharedPreferences.java

+105-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import androidx.annotation.NonNull;
88

9+
import com.termux.shared.data.DataUtils;
910
import com.termux.shared.logger.Logger;
1011
import com.termux.shared.packages.PackageUtils;
1112
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_FLOAT_APP;
@@ -18,13 +19,20 @@ public class TermuxFloatAppSharedPreferences {
1819

1920
private final Context mContext;
2021
private final SharedPreferences mSharedPreferences;
22+
private final SharedPreferences mMultiProcessSharedPreferences;
2123

24+
private int MIN_FONTSIZE;
25+
private int MAX_FONTSIZE;
26+
private int DEFAULT_FONTSIZE;
2227

2328
private static final String LOG_TAG = "TermuxFloatAppSharedPreferences";
2429

2530
private TermuxFloatAppSharedPreferences(@Nonnull Context context) {
2631
mContext = context;
2732
mSharedPreferences = getPrivateSharedPreferences(mContext);
33+
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
34+
35+
setFontVariables(context);
2836
}
2937

3038
/**
@@ -36,11 +44,11 @@ private TermuxFloatAppSharedPreferences(@Nonnull Context context) {
3644
*/
3745
@Nullable
3846
public static TermuxFloatAppSharedPreferences build(@NonNull final Context context) {
39-
Context termuxTaskerPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME);
40-
if (termuxTaskerPackageContext == null)
47+
Context termuxFloatPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME);
48+
if (termuxFloatPackageContext == null)
4149
return null;
4250
else
43-
return new TermuxFloatAppSharedPreferences(termuxTaskerPackageContext);
51+
return new TermuxFloatAppSharedPreferences(termuxFloatPackageContext);
4452
}
4553

4654
/**
@@ -53,27 +61,114 @@ public static TermuxFloatAppSharedPreferences build(@NonNull final Context conte
5361
* @return Returns the {@link TermuxFloatAppSharedPreferences}. This will {@code null} if an exception is raised.
5462
*/
5563
public static TermuxFloatAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) {
56-
Context termuxTaskerPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME, exitAppOnError);
57-
if (termuxTaskerPackageContext == null)
64+
Context termuxFloatPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_FLOAT_PACKAGE_NAME, exitAppOnError);
65+
if (termuxFloatPackageContext == null)
5866
return null;
5967
else
60-
return new TermuxFloatAppSharedPreferences(termuxTaskerPackageContext);
68+
return new TermuxFloatAppSharedPreferences(termuxFloatPackageContext);
6169
}
6270

6371
private static SharedPreferences getPrivateSharedPreferences(Context context) {
6472
if (context == null) return null;
6573
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_FLOAT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
6674
}
6775

76+
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
77+
if (context == null) return null;
78+
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_FLOAT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
79+
}
80+
81+
82+
83+
public int getWindowX() {
84+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_X, 200);
85+
86+
}
87+
88+
public void setWindowX(int value) {
89+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_X, value, false);
90+
}
91+
92+
public int getWindowY() {
93+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_Y, 200);
94+
95+
}
96+
97+
public void setWindowY(int value) {
98+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_Y, value, false);
99+
}
100+
101+
102+
103+
public int getWindowWidth() {
104+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_WIDTH, 500);
105+
106+
}
107+
108+
public void setWindowWidth(int value) {
109+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_WIDTH, value, false);
110+
}
111+
112+
public int getWindowHeight() {
113+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_HEIGHT, 500);
68114

115+
}
69116

70-
public int getLogLevel() {
71-
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
117+
public void setWindowHeight(int value) {
118+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_WINDOW_HEIGHT, value, false);
72119
}
73120

74-
public void setLogLevel(Context context, int logLevel) {
121+
122+
123+
public void setFontVariables(Context context) {
124+
int[] sizes = TermuxAppSharedPreferences.getDefaultFontSizes(context);
125+
126+
DEFAULT_FONTSIZE = sizes[0];
127+
MIN_FONTSIZE = sizes[1];
128+
MAX_FONTSIZE = sizes[2];
129+
}
130+
131+
public int getFontSize() {
132+
int fontSize = SharedPreferenceUtils.getIntStoredAsString(mSharedPreferences, TERMUX_FLOAT_APP.KEY_FONTSIZE, DEFAULT_FONTSIZE);
133+
return DataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
134+
}
135+
136+
public void setFontSize(int value) {
137+
SharedPreferenceUtils.setIntStoredAsString(mSharedPreferences, TERMUX_FLOAT_APP.KEY_FONTSIZE, value, false);
138+
}
139+
140+
public void changeFontSize(boolean increase) {
141+
int fontSize = getFontSize();
142+
143+
fontSize += (increase ? 1 : -1) * 2;
144+
fontSize = Math.max(MIN_FONTSIZE, Math.min(fontSize, MAX_FONTSIZE));
145+
146+
setFontSize(fontSize);
147+
}
148+
149+
150+
public int getLogLevel(boolean readFromFile) {
151+
if (readFromFile)
152+
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
153+
else
154+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
155+
}
156+
157+
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
75158
logLevel = Logger.setLogLevel(context, logLevel);
76-
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, logLevel, false);
159+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_FLOAT_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
160+
}
161+
162+
163+
public boolean isTerminalViewKeyLoggingEnabled(boolean readFromFile) {
164+
if (readFromFile)
165+
return SharedPreferenceUtils.getBoolean(mMultiProcessSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_FLOAT_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
166+
else
167+
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, TERMUX_FLOAT_APP.DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED);
168+
}
169+
170+
public void setTerminalViewKeyLoggingEnabled(boolean value, boolean commitToFile) {
171+
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_FLOAT_APP.KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED, value, commitToFile);
77172
}
78173

79174
}

termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxPreferenceConstants.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.termux.shared.settings.preferences;
22

33
/*
4-
* Version: v0.12.0
4+
* Version: v0.13.0
55
*
66
* Changelog
77
*
@@ -53,6 +53,11 @@
5353
* - Added `TERMUX_API_APP.KEY_LOG_LEVEL`, `TERMUX_BOOT_APP.KEY_LOG_LEVEL`,
5454
* `TERMUX_FLOAT_APP.KEY_LOG_LEVEL`, `TERMUX_STYLING_APP.KEY_LOG_LEVEL`,
5555
* `TERMUX_Widget_APP.KEY_LOG_LEVEL`.
56+
*
57+
* - 0.13.0 (2021-09-02)
58+
* - Added following to `TERMUX_FLOAT_APP`:
59+
* `KEY_WINDOW_X`, `KEY_WINDOW_Y`, `KEY_WINDOW_WIDTH`, `KEY_WINDOW_HEIGHT`, `KEY_FONTSIZE`,
60+
* `KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED`.
5661
*/
5762

5863
/**
@@ -187,11 +192,42 @@ public static final class TERMUX_BOOT_APP {
187192
*/
188193
public static final class TERMUX_FLOAT_APP {
189194

195+
/**
196+
* The float window x coordinate.
197+
*/
198+
public static final String KEY_WINDOW_X = "window_x";
199+
200+
/**
201+
* The float window y coordinate.
202+
*/
203+
public static final String KEY_WINDOW_Y = "window_y";
204+
205+
/**
206+
* The float window width.
207+
*/
208+
public static final String KEY_WINDOW_WIDTH = "window_width";
209+
210+
/**
211+
* The float window height.
212+
*/
213+
public static final String KEY_WINDOW_HEIGHT = "window_height";
214+
215+
/**
216+
* Defines the key for font size of termux terminal view.
217+
*/
218+
public static final String KEY_FONTSIZE = "fontsize";
219+
190220
/**
191221
* Defines the key for current log level.
192222
*/
193223
public static final String KEY_LOG_LEVEL = "log_level";
194224

225+
/**
226+
* Defines the key for whether termux terminal view key logging is enabled or not
227+
*/
228+
public static final String KEY_TERMINAL_VIEW_KEY_LOGGING_ENABLED = "terminal_view_key_logging_enabled";
229+
public static final boolean DEFAULT_VALUE_TERMINAL_VIEW_KEY_LOGGING_ENABLED = false;
230+
195231
}
196232

197233

termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxTaskerAppSharedPreferences.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Cont
7474

7575

7676

77-
public int getLogLevel(boolean readFromFfile) {
78-
if (readFromFfile)
77+
public int getLogLevel(boolean readFromFile) {
78+
if (readFromFile)
7979
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
8080
else
8181
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);

termux-shared/src/main/java/com/termux/shared/view/ViewUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ public static int getDisplayOrientation(@NonNull Context context) {
173173
/**
174174
* Get device display size.
175175
*
176-
* @param context The {@link Context} to check with.
176+
* @param context The {@link Context} to check with. It must be {@link Activity} context, otherwise
177+
* android will throw:
178+
* `java.lang.IllegalArgumentException: Used non-visual Context to obtain an instance of WindowManager. Please use an Activity or a ContextWrapper around one instead.`
177179
* @param activitySize The set to {@link true}, then size returned will be that of the activity
178180
* and can be smaller than physical display size in multi-window mode.
179181
* @return Returns the display size as {@link Point}.

0 commit comments

Comments
 (0)