Skip to content

Commit

Permalink
Add support for setting Termux:Tasker log level from TermuxSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed May 14, 2021
1 parent 6de3713 commit 339b2a2
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 22 deletions.
41 changes: 27 additions & 14 deletions app/src/main/java/com/termux/app/activities/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -74,25 +88,24 @@ 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
// since Termux isn't exempted from the playstore policy donation links restriction
// 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;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
<string name="termux_soft_keyboard_enabled_only_if_no_hardware_on">Soft keyboard will be enabled only if no hardware keyboard is connected.</string>


<!-- Termux Tasker App Preferences -->
<string name="termux_tasker_preferences_title">&TERMUX_TASKER_APP_NAME;</string>
<string name="termux_tasker_preferences_summary">Preferences for &TERMUX_TASKER_APP_NAME; app</string>


<!-- About Preference -->
<string name="about_preference_title">About</string>

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
app:summary="@string/termux_preferences_summary"
app:fragment="com.termux.app.fragments.settings.TermuxPreferencesFragment"/>

<Preference
app:key="termux_tasker"
app:title="@string/termux_tasker_preferences_title"
app:summary="@string/termux_tasker_preferences_summary"
app:isPreferenceVisible="false"
app:fragment="com.termux.app.fragments.settings.TermuxTaskerPreferencesFragment"/>

<Preference
app:key="about"
app:title="@string/about_preference_title"
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/xml/termux_tasker_debugging_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:key="logging"
app:title="@string/termux_logging_header">

<ListPreference
app:defaultValue="1"
app:key="log_level"
app:title="@string/termux_log_level_title"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

</PreferenceScreen>
8 changes: 8 additions & 0 deletions app/src/main/res/xml/termux_tasker_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<Preference
app:title="@string/termux_debugging_preferences_title"
app:summary="@string/termux_debugging_preferences_summary"
app:fragment="com.termux.app.fragments.settings.termux_tasker.DebuggingPreferencesFragment"/>

</PreferenceScreen>

0 comments on commit 339b2a2

Please sign in to comment.