Skip to content

Commit 4953b12

Browse files
Added: Add log level setting in Termux Settings for termux-widget
1 parent d5ffb11 commit 4953b12

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.termux.shared.settings.preferences.TermuxAPIAppSharedPreferences;
2121
import com.termux.shared.settings.preferences.TermuxFloatAppSharedPreferences;
2222
import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences;
23+
import com.termux.shared.settings.preferences.TermuxWidgetAppSharedPreferences;
2324
import com.termux.shared.termux.AndroidUtils;
2425
import com.termux.shared.termux.TermuxConstants;
2526
import com.termux.shared.termux.TermuxUtils;
@@ -60,6 +61,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
6061
configureTermuxAPIPreference(context);
6162
configureTermuxFloatPreference(context);
6263
configureTermuxTaskerPreference(context);
64+
configureTermuxWidgetPreference(context);
6365
configureAboutPreference(context);
6466
configureDonatePreference(context);
6567
}
@@ -91,6 +93,15 @@ private void configureTermuxTaskerPreference(@NonNull Context context) {
9193
}
9294
}
9395

96+
private void configureTermuxWidgetPreference(@NonNull Context context) {
97+
Preference termuxWidgetPreference = findPreference("termux_widget");
98+
if (termuxWidgetPreference != null) {
99+
TermuxWidgetAppSharedPreferences preferences = TermuxWidgetAppSharedPreferences.build(context, false);
100+
// If failed to get app preferences, then likely app is not installed, so do not show its preference
101+
termuxWidgetPreference.setVisible(preferences != null);
102+
}
103+
}
104+
94105
private void configureAboutPreference(@NonNull Context context) {
95106
Preference aboutPreference = findPreference("about");
96107
if (aboutPreference != null) {
Lines changed: 49 additions & 0 deletions
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.TermuxWidgetAppSharedPreferences;
13+
14+
@Keep
15+
public class TermuxWidgetPreferencesFragment 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(TermuxWidgetPreferencesDataStore.getInstance(context));
24+
25+
setPreferencesFromResource(R.xml.termux_widget_preferences, rootKey);
26+
}
27+
28+
}
29+
30+
class TermuxWidgetPreferencesDataStore extends PreferenceDataStore {
31+
32+
private final Context mContext;
33+
private final TermuxWidgetAppSharedPreferences mPreferences;
34+
35+
private static TermuxWidgetPreferencesDataStore mInstance;
36+
37+
private TermuxWidgetPreferencesDataStore(Context context) {
38+
mContext = context;
39+
mPreferences = TermuxWidgetAppSharedPreferences.build(context, true);
40+
}
41+
42+
public static synchronized TermuxWidgetPreferencesDataStore getInstance(Context context) {
43+
if (mInstance == null) {
44+
mInstance = new TermuxWidgetPreferencesDataStore(context);
45+
}
46+
return mInstance;
47+
}
48+
49+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.termux.app.fragments.settings.termux_widget;
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.TermuxWidgetAppSharedPreferences;
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_widget_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+
TermuxWidgetAppSharedPreferences preferences = TermuxWidgetAppSharedPreferences.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 TermuxWidgetAppSharedPreferences mPreferences;
54+
55+
private static DebuggingPreferencesDataStore mInstance;
56+
57+
private DebuggingPreferencesDataStore(Context context) {
58+
mContext = context;
59+
mPreferences = TermuxWidgetAppSharedPreferences.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+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@
211211

212212

213213

214+
<!-- Termux Widget App Preferences -->
215+
<string name="termux_widget_preferences_title">&TERMUX_WIDGET_APP_NAME;</string>
216+
<string name="termux_widget_preferences_summary">Preferences for &TERMUX_WIDGET_APP_NAME; app</string>
217+
218+
219+
214220
<!-- About Preference -->
215221
<string name="about_preference_title">About</string>
216222

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
app:isPreferenceVisible="false"
2828
app:fragment="com.termux.app.fragments.settings.TermuxTaskerPreferencesFragment"/>
2929

30+
<Preference
31+
app:key="termux_widget"
32+
app:title="@string/termux_widget_preferences_title"
33+
app:summary="@string/termux_widget_preferences_summary"
34+
app:isPreferenceVisible="false"
35+
app:fragment="com.termux.app.fragments.settings.TermuxWidgetPreferencesFragment"/>
36+
3037
<Preference
3138
app:key="about"
3239
app:title="@string/about_preference_title"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
</PreferenceCategory>
14+
15+
</PreferenceScreen>
Lines changed: 8 additions & 0 deletions
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_widget.DebuggingPreferencesFragment"/>
7+
8+
</PreferenceScreen>

0 commit comments

Comments
 (0)