Skip to content

Commit 582e569

Browse files
Added: Add SharedPrefernces controllers for all current published termux plugin app
Also added log level setting in Termux Settings for Termux:API. Others can be added when logging is implemented in the plugin apps via `Logger` class provided by `termux-shared`.
1 parent 5a8c4f1 commit 582e569

14 files changed

+677
-7
lines changed

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.termux.app.models.UserAction;
1818
import com.termux.shared.interact.ShareUtils;
1919
import com.termux.shared.packages.PackageUtils;
20+
import com.termux.shared.settings.preferences.TermuxAPIAppSharedPreferences;
2021
import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences;
2122
import com.termux.shared.termux.AndroidUtils;
2223
import com.termux.shared.termux.TermuxConstants;
@@ -55,17 +56,27 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
5556

5657
setPreferencesFromResource(R.xml.root_preferences, rootKey);
5758

59+
configureTermuxAPIPreference(context);
5860
configureTermuxTaskerPreference(context);
5961
configureAboutPreference(context);
6062
configureDonatePreference(context);
6163
}
6264

65+
private void configureTermuxAPIPreference(@NonNull Context context) {
66+
Preference termuxAPIPreference = findPreference("termux_api");
67+
if (termuxAPIPreference != null) {
68+
TermuxAPIAppSharedPreferences preferences = TermuxAPIAppSharedPreferences.build(context, false);
69+
// If failed to get app preferences, then likely app is not installed, so do not show its preference
70+
termuxAPIPreference.setVisible(preferences != null);
71+
}
72+
}
73+
6374
private void configureTermuxTaskerPreference(@NonNull Context context) {
64-
Preference termuxTaskerPrefernce = findPreference("termux_tasker");
65-
if (termuxTaskerPrefernce != null) {
75+
Preference termuxTaskerPreference = findPreference("termux_tasker");
76+
if (termuxTaskerPreference != null) {
6677
TermuxTaskerAppSharedPreferences preferences = TermuxTaskerAppSharedPreferences.build(context, false);
6778
// If failed to get app preferences, then likely app is not installed, so do not show its preference
68-
termuxTaskerPrefernce.setVisible(preferences != null);
79+
termuxTaskerPreference.setVisible(preferences != null);
6980
}
7081
}
7182

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.TermuxAPIAppSharedPreferences;
13+
14+
@Keep
15+
public class TermuxAPIPreferencesFragment 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(TermuxAPIPreferencesDataStore.getInstance(context));
24+
25+
setPreferencesFromResource(R.xml.termux_api_preferences, rootKey);
26+
}
27+
28+
}
29+
30+
class TermuxAPIPreferencesDataStore extends PreferenceDataStore {
31+
32+
private final Context mContext;
33+
private final TermuxAPIAppSharedPreferences mPreferences;
34+
35+
private static TermuxAPIPreferencesDataStore mInstance;
36+
37+
private TermuxAPIPreferencesDataStore(Context context) {
38+
mContext = context;
39+
mPreferences = TermuxAPIAppSharedPreferences.build(context, true);
40+
}
41+
42+
public static synchronized TermuxAPIPreferencesDataStore getInstance(Context context) {
43+
if (mInstance == null) {
44+
mInstance = new TermuxAPIPreferencesDataStore(context);
45+
}
46+
return mInstance;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.termux.app.fragments.settings.termux_api;
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.TermuxAPIAppSharedPreferences;
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_api_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+
TermuxAPIAppSharedPreferences preferences = TermuxAPIAppSharedPreferences.build(context, true);
41+
if (preferences == null) return;
42+
43+
com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment.
44+
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel());
45+
loggingCategory.addPreference(logLevelListPreference);
46+
}
47+
}
48+
}
49+
50+
class DebuggingPreferencesDataStore extends PreferenceDataStore {
51+
52+
private final Context mContext;
53+
private final TermuxAPIAppSharedPreferences mPreferences;
54+
55+
private static DebuggingPreferencesDataStore mInstance;
56+
57+
private DebuggingPreferencesDataStore(Context context) {
58+
mContext = context;
59+
mPreferences = TermuxAPIAppSharedPreferences.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());
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));
94+
}
95+
break;
96+
default:
97+
break;
98+
}
99+
}
100+
101+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@
183183

184184

185185

186+
<!-- Termux API App Preferences -->
187+
<string name="termux_api_preferences_title">&TERMUX_API_APP_NAME;</string>
188+
<string name="termux_api_preferences_summary">Preferences for &TERMUX_API_APP_NAME; app</string>
189+
190+
191+
186192
<!-- Termux Tasker App Preferences -->
187193
<string name="termux_tasker_preferences_title">&TERMUX_TASKER_APP_NAME;</string>
188194
<string name="termux_tasker_preferences_summary">Preferences for &TERMUX_TASKER_APP_NAME; app</string>
189195

190196

197+
191198
<!-- About Preference -->
192199
<string name="about_preference_title">About</string>
193200

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

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
app:summary="@string/termux_preferences_summary"
77
app:fragment="com.termux.app.fragments.settings.TermuxPreferencesFragment"/>
88

9+
<Preference
10+
app:key="termux_api"
11+
app:title="@string/termux_api_preferences_title"
12+
app:summary="@string/termux_api_preferences_summary"
13+
app:isPreferenceVisible="false"
14+
app:fragment="com.termux.app.fragments.settings.TermuxAPIPreferencesFragment"/>
15+
916
<Preference
1017
app:key="termux_tasker"
1118
app:title="@string/termux_tasker_preferences_title"
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>
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_api.DebuggingPreferencesFragment"/>
7+
8+
</PreferenceScreen>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.termux.shared.settings.preferences;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.SharedPreferences;
6+
7+
import androidx.annotation.NonNull;
8+
9+
import com.termux.shared.logger.Logger;
10+
import com.termux.shared.packages.PackageUtils;
11+
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_API_APP;
12+
import com.termux.shared.termux.TermuxConstants;
13+
14+
import javax.annotation.Nonnull;
15+
import javax.annotation.Nullable;
16+
17+
public class TermuxAPIAppSharedPreferences {
18+
19+
private final Context mContext;
20+
private final SharedPreferences mSharedPreferences;
21+
22+
23+
private static final String LOG_TAG = "TermuxAPIAppSharedPreferences";
24+
25+
private TermuxAPIAppSharedPreferences(@Nonnull Context context) {
26+
mContext = context;
27+
mSharedPreferences = getPrivateSharedPreferences(mContext);
28+
}
29+
30+
/**
31+
* Get the {@link Context} for a package name.
32+
*
33+
* @param context The {@link Context} to use to get the {@link Context} of the
34+
* {@link TermuxConstants#TERMUX_API_PACKAGE_NAME}.
35+
* @return Returns the {@link TermuxAPIAppSharedPreferences}. This will {@code null} if an exception is raised.
36+
*/
37+
@Nullable
38+
public static TermuxAPIAppSharedPreferences build(@NonNull final Context context) {
39+
Context termuxTaskerPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_API_PACKAGE_NAME);
40+
if (termuxTaskerPackageContext == null)
41+
return null;
42+
else
43+
return new TermuxAPIAppSharedPreferences(termuxTaskerPackageContext);
44+
}
45+
46+
/**
47+
* Get the {@link Context} for a package name.
48+
*
49+
* @param context The {@link Activity} to use to get the {@link Context} of the
50+
* {@link TermuxConstants#TERMUX_API_PACKAGE_NAME}.
51+
* @param exitAppOnError If {@code true} and failed to get package context, then a dialog will
52+
* be shown which when dismissed will exit the app.
53+
* @return Returns the {@link TermuxAPIAppSharedPreferences}. This will {@code null} if an exception is raised.
54+
*/
55+
public static TermuxAPIAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) {
56+
Context termuxTaskerPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_API_PACKAGE_NAME, exitAppOnError);
57+
if (termuxTaskerPackageContext == null)
58+
return null;
59+
else
60+
return new TermuxAPIAppSharedPreferences(termuxTaskerPackageContext);
61+
}
62+
63+
private static SharedPreferences getPrivateSharedPreferences(Context context) {
64+
if (context == null) return null;
65+
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
66+
}
67+
68+
69+
70+
public int getLogLevel() {
71+
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
72+
}
73+
74+
public void setLogLevel(Context context, int logLevel) {
75+
logLevel = Logger.setLogLevel(context, logLevel);
76+
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, false);
77+
}
78+
79+
}

0 commit comments

Comments
 (0)