diff --git a/app/src/main/java/com/termux/app/activities/SettingsActivity.java b/app/src/main/java/com/termux/app/activities/SettingsActivity.java index 08ac7fef59..5ef65a3d00 100644 --- a/app/src/main/java/com/termux/app/activities/SettingsActivity.java +++ b/app/src/main/java/com/termux/app/activities/SettingsActivity.java @@ -3,6 +3,7 @@ import android.content.Context; import android.os.Bundle; +import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.Preference; @@ -13,6 +14,7 @@ import com.termux.app.models.UserAction; import com.termux.shared.interact.ShareUtils; import com.termux.shared.packages.PackageUtils; +import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences; import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.TermuxUtils; @@ -44,17 +46,29 @@ public boolean onSupportNavigateUp() { public static class RootPreferencesFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + setPreferencesFromResource(R.xml.root_preferences, rootKey); - configureAboutPreference(); - configureDonatePreference(); + configureTermuxTaskerPreference(context); + configureAboutPreference(context); + configureDonatePreference(context); } - private void configureAboutPreference() { - Context context = getContext(); - Preference about = findPreference("about"); - if (context != null && about != null) { - about.setOnPreferenceClickListener(preference -> { + private void configureTermuxTaskerPreference(@NonNull Context context) { + Preference termuxTaskerPrefernce = findPreference("termux_tasker"); + if (termuxTaskerPrefernce != null) { + TermuxTaskerAppSharedPreferences preferences = TermuxTaskerAppSharedPreferences.build(context, false); + // If failed to get app preferences, then likely app is not installed, so do not show its preference + termuxTaskerPrefernce.setVisible(preferences != null); + } + } + + private void configureAboutPreference(@NonNull Context context) { + Preference aboutPreference = findPreference("about"); + if (aboutPreference != null) { + aboutPreference.setOnPreferenceClickListener(preference -> { String title = "About"; StringBuilder aboutString = new StringBuilder(); @@ -74,10 +88,9 @@ private void configureAboutPreference() { } } - private void configureDonatePreference() { - Context context = getContext(); - Preference donate = findPreference("donate"); - if (context != null && donate != null) { + private void configureDonatePreference(@NonNull Context context) { + Preference donatePreference = findPreference("donate"); + if (donatePreference != null) { String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(context); if (signingCertificateSHA256Digest != null) { // If APK is a Google Playstore release, then do not show the donation link @@ -85,14 +98,14 @@ private void configureDonatePreference() { // Check Fund solicitations: https://pay.google.com/intl/en_in/about/policy/ String apkRelease = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest); if (apkRelease == null || apkRelease.equals(TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST)) { - donate.setVisible(false); + donatePreference.setVisible(false); return; } else { - donate.setVisible(true); + donatePreference.setVisible(true); } } - donate.setOnPreferenceClickListener(preference -> { + donatePreference.setOnPreferenceClickListener(preference -> { ShareUtils.openURL(context, TermuxConstants.TERMUX_DONATE_URL); return true; }); diff --git a/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java index b92472c672..273fa0f552 100644 --- a/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java +++ b/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java @@ -16,8 +16,11 @@ public class TermuxPreferencesFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + PreferenceManager preferenceManager = getPreferenceManager(); - preferenceManager.setPreferenceDataStore(TermuxPreferencesDataStore.getInstance(getContext())); + preferenceManager.setPreferenceDataStore(TermuxPreferencesDataStore.getInstance(context)); setPreferencesFromResource(R.xml.termux_preferences, rootKey); } diff --git a/app/src/main/java/com/termux/app/fragments/settings/TermuxTaskerPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/TermuxTaskerPreferencesFragment.java new file mode 100644 index 0000000000..b86685b60b --- /dev/null +++ b/app/src/main/java/com/termux/app/fragments/settings/TermuxTaskerPreferencesFragment.java @@ -0,0 +1,49 @@ +package com.termux.app.fragments.settings; + +import android.content.Context; +import android.os.Bundle; + +import androidx.annotation.Keep; +import androidx.preference.PreferenceDataStore; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceManager; + +import com.termux.R; +import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences; + +@Keep +public class TermuxTaskerPreferencesFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + + PreferenceManager preferenceManager = getPreferenceManager(); + preferenceManager.setPreferenceDataStore(TermuxTaskerPreferencesDataStore.getInstance(context)); + + setPreferencesFromResource(R.xml.termux_tasker_preferences, rootKey); + } + +} + +class TermuxTaskerPreferencesDataStore extends PreferenceDataStore { + + private final Context mContext; + private final TermuxTaskerAppSharedPreferences mPreferences; + + private static TermuxTaskerPreferencesDataStore mInstance; + + private TermuxTaskerPreferencesDataStore(Context context) { + mContext = context; + mPreferences = TermuxTaskerAppSharedPreferences.build(context, true); + } + + public static synchronized TermuxTaskerPreferencesDataStore getInstance(Context context) { + if (mInstance == null) { + mInstance = new TermuxTaskerPreferencesDataStore(context); + } + return mInstance; + } + +} diff --git a/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java index dd5795eb01..72ced348a6 100644 --- a/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java +++ b/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java @@ -4,6 +4,7 @@ import android.os.Bundle; import androidx.annotation.Keep; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.preference.ListPreference; import androidx.preference.PreferenceCategory; @@ -20,20 +21,32 @@ public class DebuggingPreferencesFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + PreferenceManager preferenceManager = getPreferenceManager(); - preferenceManager.setPreferenceDataStore(DebuggingPreferencesDataStore.getInstance(getContext())); + preferenceManager.setPreferenceDataStore(DebuggingPreferencesDataStore.getInstance(context)); setPreferencesFromResource(R.xml.termux_debugging_preferences, rootKey); + configureLoggingPreferences(context); + } + + private void configureLoggingPreferences(@NonNull Context context) { PreferenceCategory loggingCategory = findPreference("logging"); + if (loggingCategory == null) return; + + ListPreference logLevelListPreference = findPreference("log_level"); + if (logLevelListPreference != null) { + TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context, true); + if (preferences == null) return; - if (loggingCategory != null) { - final ListPreference logLevelListPreference = setLogLevelListPreferenceData(findPreference("log_level"), getContext()); + setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel()); loggingCategory.addPreference(logLevelListPreference); } } - protected ListPreference setLogLevelListPreferenceData(ListPreference logLevelListPreference, Context context) { + public static ListPreference setLogLevelListPreferenceData(ListPreference logLevelListPreference, Context context, int logLevel) { if (logLevelListPreference == null) logLevelListPreference = new ListPreference(context); @@ -43,8 +56,8 @@ protected ListPreference setLogLevelListPreferenceData(ListPreference logLevelLi logLevelListPreference.setEntryValues(logLevels); logLevelListPreference.setEntries(logLevelLabels); - logLevelListPreference.setValue(String.valueOf(Logger.getLogLevel())); - logLevelListPreference.setDefaultValue(Logger.getLogLevel()); + logLevelListPreference.setValue(String.valueOf(logLevel)); + logLevelListPreference.setDefaultValue(Logger.DEFAULT_LOG_LEVEL); return logLevelListPreference; } diff --git a/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java index 109daf9569..46e7504940 100644 --- a/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java +++ b/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java @@ -16,8 +16,11 @@ public class TerminalIOPreferencesFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + PreferenceManager preferenceManager = getPreferenceManager(); - preferenceManager.setPreferenceDataStore(TerminalIOPreferencesDataStore.getInstance(getContext())); + preferenceManager.setPreferenceDataStore(TerminalIOPreferencesDataStore.getInstance(context)); setPreferencesFromResource(R.xml.termux_terminal_io_preferences, rootKey); } diff --git a/app/src/main/java/com/termux/app/fragments/settings/termux_tasker/DebuggingPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/termux_tasker/DebuggingPreferencesFragment.java new file mode 100644 index 0000000000..a66708140f --- /dev/null +++ b/app/src/main/java/com/termux/app/fragments/settings/termux_tasker/DebuggingPreferencesFragment.java @@ -0,0 +1,101 @@ +package com.termux.app.fragments.settings.termux_tasker; + +import android.content.Context; +import android.os.Bundle; + +import androidx.annotation.Keep; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.preference.ListPreference; +import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceDataStore; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceManager; + +import com.termux.R; +import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences; + +@Keep +public class DebuggingPreferencesFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + Context context = getContext(); + if (context == null) return; + + PreferenceManager preferenceManager = getPreferenceManager(); + preferenceManager.setPreferenceDataStore(DebuggingPreferencesDataStore.getInstance(context)); + + setPreferencesFromResource(R.xml.termux_tasker_debugging_preferences, rootKey); + + configureLoggingPreferences(context); + } + + private void configureLoggingPreferences(@NonNull Context context) { + PreferenceCategory loggingCategory = findPreference("logging"); + if (loggingCategory == null) return; + + ListPreference logLevelListPreference = findPreference("log_level"); + if (logLevelListPreference != null) { + TermuxTaskerAppSharedPreferences preferences = TermuxTaskerAppSharedPreferences.build(context, true); + if (preferences == null) return; + + com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment. + setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel(true)); + loggingCategory.addPreference(logLevelListPreference); + } + } +} + +class DebuggingPreferencesDataStore extends PreferenceDataStore { + + private final Context mContext; + private final TermuxTaskerAppSharedPreferences mPreferences; + + private static DebuggingPreferencesDataStore mInstance; + + private DebuggingPreferencesDataStore(Context context) { + mContext = context; + mPreferences = TermuxTaskerAppSharedPreferences.build(context, true); + } + + public static synchronized DebuggingPreferencesDataStore getInstance(Context context) { + if (mInstance == null) { + mInstance = new DebuggingPreferencesDataStore(context); + } + return mInstance; + } + + + + @Override + @Nullable + public String getString(String key, @Nullable String defValue) { + if (mPreferences == null) return null; + if (key == null) return null; + + switch (key) { + case "log_level": + return String.valueOf(mPreferences.getLogLevel(true)); + default: + return null; + } + } + + @Override + public void putString(String key, @Nullable String value) { + if (mPreferences == null) return; + if (key == null) return; + + switch (key) { + case "log_level": + if (value != null) { + mPreferences.setLogLevel(mContext, Integer.parseInt(value), true); + } + break; + default: + break; + } + } + +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c149c4cba3..fe0851c995 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -170,6 +170,11 @@ Soft keyboard will be enabled only if no hardware keyboard is connected. + + &TERMUX_TASKER_APP_NAME; + Preferences for &TERMUX_TASKER_APP_NAME; app + + About diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index f8efef7b70..39c6093ed0 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -6,6 +6,13 @@ app:summary="@string/termux_preferences_summary" app:fragment="com.termux.app.fragments.settings.TermuxPreferencesFragment"/> + + + + + + + + + + diff --git a/app/src/main/res/xml/termux_tasker_preferences.xml b/app/src/main/res/xml/termux_tasker_preferences.xml new file mode 100644 index 0000000000..9aa72485bc --- /dev/null +++ b/app/src/main/res/xml/termux_tasker_preferences.xml @@ -0,0 +1,8 @@ + + + + +