Skip to content

Commit d55c100

Browse files
Added: Add termux-float log level settings in termux app settings
1 parent 36557b2 commit d55c100

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

app/src/main/java/com/termux/app/activities/SettingsActivity.java

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.termux.shared.interact.ShareUtils;
1919
import com.termux.shared.packages.PackageUtils;
2020
import com.termux.shared.settings.preferences.TermuxAPIAppSharedPreferences;
21+
import com.termux.shared.settings.preferences.TermuxFloatAppSharedPreferences;
2122
import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences;
2223
import com.termux.shared.termux.AndroidUtils;
2324
import com.termux.shared.termux.TermuxConstants;
@@ -57,6 +58,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
5758
setPreferencesFromResource(R.xml.root_preferences, rootKey);
5859

5960
configureTermuxAPIPreference(context);
61+
configureTermuxFloatPreference(context);
6062
configureTermuxTaskerPreference(context);
6163
configureAboutPreference(context);
6264
configureDonatePreference(context);
@@ -71,6 +73,15 @@ private void configureTermuxAPIPreference(@NonNull Context context) {
7173
}
7274
}
7375

76+
private void configureTermuxFloatPreference(@NonNull Context context) {
77+
Preference termuxFloatPreference = findPreference("termux_float");
78+
if (termuxFloatPreference != null) {
79+
TermuxFloatAppSharedPreferences preferences = TermuxFloatAppSharedPreferences.build(context, false);
80+
// If failed to get app preferences, then likely app is not installed, so do not show its preference
81+
termuxFloatPreference.setVisible(preferences != null);
82+
}
83+
}
84+
7485
private void configureTermuxTaskerPreference(@NonNull Context context) {
7586
Preference termuxTaskerPreference = findPreference("termux_tasker");
7687
if (termuxTaskerPreference != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.termux.app.fragments.settings;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
6+
import androidx.annotation.Keep;
7+
import androidx.preference.PreferenceDataStore;
8+
import androidx.preference.PreferenceFragmentCompat;
9+
import androidx.preference.PreferenceManager;
10+
11+
import com.termux.R;
12+
import com.termux.shared.settings.preferences.TermuxFloatAppSharedPreferences;
13+
14+
@Keep
15+
public class TermuxFloatPreferencesFragment extends PreferenceFragmentCompat {
16+
17+
@Override
18+
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
19+
Context context = getContext();
20+
if (context == null) return;
21+
22+
PreferenceManager preferenceManager = getPreferenceManager();
23+
preferenceManager.setPreferenceDataStore(TermuxFloatPreferencesDataStore.getInstance(context));
24+
25+
setPreferencesFromResource(R.xml.termux_float_preferences, rootKey);
26+
}
27+
28+
}
29+
30+
class TermuxFloatPreferencesDataStore extends PreferenceDataStore {
31+
32+
private final Context mContext;
33+
private final TermuxFloatAppSharedPreferences mPreferences;
34+
35+
private static TermuxFloatPreferencesDataStore mInstance;
36+
37+
private TermuxFloatPreferencesDataStore(Context context) {
38+
mContext = context;
39+
mPreferences = TermuxFloatAppSharedPreferences.build(context, true);
40+
}
41+
42+
public static synchronized TermuxFloatPreferencesDataStore getInstance(Context context) {
43+
if (mInstance == null) {
44+
mInstance = new TermuxFloatPreferencesDataStore(context);
45+
}
46+
return mInstance;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.termux.app.fragments.settings.termux_float;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
6+
import androidx.annotation.Keep;
7+
import androidx.annotation.NonNull;
8+
import androidx.annotation.Nullable;
9+
import androidx.preference.ListPreference;
10+
import androidx.preference.PreferenceCategory;
11+
import androidx.preference.PreferenceDataStore;
12+
import androidx.preference.PreferenceFragmentCompat;
13+
import androidx.preference.PreferenceManager;
14+
15+
import com.termux.R;
16+
import com.termux.shared.settings.preferences.TermuxFloatAppSharedPreferences;
17+
18+
@Keep
19+
public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {
20+
21+
@Override
22+
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
23+
Context context = getContext();
24+
if (context == null) return;
25+
26+
PreferenceManager preferenceManager = getPreferenceManager();
27+
preferenceManager.setPreferenceDataStore(DebuggingPreferencesDataStore.getInstance(context));
28+
29+
setPreferencesFromResource(R.xml.termux_float_debugging_preferences, rootKey);
30+
31+
configureLoggingPreferences(context);
32+
}
33+
34+
private void configureLoggingPreferences(@NonNull Context context) {
35+
PreferenceCategory loggingCategory = findPreference("logging");
36+
if (loggingCategory == null) return;
37+
38+
ListPreference logLevelListPreference = findPreference("log_level");
39+
if (logLevelListPreference != null) {
40+
TermuxFloatAppSharedPreferences preferences = TermuxFloatAppSharedPreferences.build(context, true);
41+
if (preferences == null) return;
42+
43+
com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment.
44+
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel(true));
45+
loggingCategory.addPreference(logLevelListPreference);
46+
}
47+
}
48+
}
49+
50+
class DebuggingPreferencesDataStore extends PreferenceDataStore {
51+
52+
private final Context mContext;
53+
private final TermuxFloatAppSharedPreferences mPreferences;
54+
55+
private static DebuggingPreferencesDataStore mInstance;
56+
57+
private DebuggingPreferencesDataStore(Context context) {
58+
mContext = context;
59+
mPreferences = TermuxFloatAppSharedPreferences.build(context, true);
60+
}
61+
62+
public static synchronized DebuggingPreferencesDataStore getInstance(Context context) {
63+
if (mInstance == null) {
64+
mInstance = new DebuggingPreferencesDataStore(context);
65+
}
66+
return mInstance;
67+
}
68+
69+
70+
71+
@Override
72+
@Nullable
73+
public String getString(String key, @Nullable String defValue) {
74+
if (mPreferences == null) return null;
75+
if (key == null) return null;
76+
77+
switch (key) {
78+
case "log_level":
79+
return String.valueOf(mPreferences.getLogLevel(true));
80+
default:
81+
return null;
82+
}
83+
}
84+
85+
@Override
86+
public void putString(String key, @Nullable String value) {
87+
if (mPreferences == null) return;
88+
if (key == null) return;
89+
90+
switch (key) {
91+
case "log_level":
92+
if (value != null) {
93+
mPreferences.setLogLevel(mContext, Integer.parseInt(value), true);
94+
}
95+
break;
96+
default:
97+
break;
98+
}
99+
}
100+
101+
@Override
102+
public void putBoolean(String key, boolean value) {
103+
if (mPreferences == null) return;
104+
if (key == null) return;
105+
106+
switch (key) {
107+
case "terminal_view_key_logging_enabled":
108+
mPreferences.setTerminalViewKeyLoggingEnabled(value, true);
109+
break;
110+
default:
111+
break;
112+
}
113+
}
114+
115+
@Override
116+
public boolean getBoolean(String key, boolean defValue) {
117+
if (mPreferences == null) return false;
118+
switch (key) {
119+
case "terminal_view_key_logging_enabled":
120+
return mPreferences.isTerminalViewKeyLoggingEnabled(true);
121+
default:
122+
return false;
123+
}
124+
}
125+
126+
}

app/src/main/res/values/strings.xml

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189

190190

191191

192+
<!-- Termux Float App Preferences -->
193+
<string name="termux_float_preferences_title">&TERMUX_FLOAT_APP_NAME;</string>
194+
<string name="termux_float_preferences_summary">Preferences for &TERMUX_FLOAT_APP_NAME; app</string>
195+
196+
197+
192198
<!-- Termux Tasker App Preferences -->
193199
<string name="termux_tasker_preferences_title">&TERMUX_TASKER_APP_NAME;</string>
194200
<string name="termux_tasker_preferences_summary">Preferences for &TERMUX_TASKER_APP_NAME; app</string>

app/src/main/res/xml/root_preferences.xml

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
app:isPreferenceVisible="false"
1414
app:fragment="com.termux.app.fragments.settings.TermuxAPIPreferencesFragment"/>
1515

16+
<Preference
17+
app:key="termux_float"
18+
app:title="@string/termux_float_preferences_title"
19+
app:summary="@string/termux_float_preferences_summary"
20+
app:isPreferenceVisible="false"
21+
app:fragment="com.termux.app.fragments.settings.TermuxFloatPreferencesFragment"/>
22+
1623
<Preference
1724
app:key="termux_tasker"
1825
app:title="@string/termux_tasker_preferences_title"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
2+
3+
<PreferenceCategory
4+
app:key="logging"
5+
app:title="@string/termux_logging_header">
6+
7+
<ListPreference
8+
app:defaultValue="1"
9+
app:key="log_level"
10+
app:title="@string/termux_log_level_title"
11+
app:useSimpleSummaryProvider="true" />
12+
13+
<SwitchPreferenceCompat
14+
app:key="terminal_view_key_logging_enabled"
15+
app:summaryOff="@string/termux_terminal_view_key_logging_enabled_off"
16+
app:summaryOn="@string/termux_terminal_view_key_logging_enabled_on"
17+
app:title="@string/termux_terminal_view_key_logging_enabled_title" />
18+
19+
</PreferenceCategory>
20+
21+
</PreferenceScreen>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
2+
3+
<Preference
4+
app:title="@string/termux_debugging_preferences_title"
5+
app:summary="@string/termux_debugging_preferences_summary"
6+
app:fragment="com.termux.app.fragments.settings.termux_float.DebuggingPreferencesFragment"/>
7+
8+
</PreferenceScreen>

0 commit comments

Comments
 (0)