Changed: Use multi-process SharedPrefernces for log level of plugin apps

Since termux-app runs in a separate process from other apps, if a user sets log level in termux settings, then it would require exiting the `termux-app` completely since android caches `SharedPrefernces` in memory and only writes to the file on app exit. Now updated value will be instantly written to the file so that plugins can directly read at startup. If plugins are already running, they would need to be restarted since usually log levels are loaded at startup.
This commit is contained in:
agnostic-apollo
2021-09-02 06:40:02 +05:00
parent d55c1001c8
commit 9f1203f049
5 changed files with 67 additions and 27 deletions

View File

@@ -41,7 +41,7 @@ public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {
if (preferences == null) return; if (preferences == null) return;
com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment. com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment.
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel()); setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel(true));
loggingCategory.addPreference(logLevelListPreference); loggingCategory.addPreference(logLevelListPreference);
} }
} }
@@ -76,7 +76,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
switch (key) { switch (key) {
case "log_level": case "log_level":
return String.valueOf(mPreferences.getLogLevel()); return String.valueOf(mPreferences.getLogLevel(true));
default: default:
return null; return null;
} }
@@ -90,7 +90,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
switch (key) { switch (key) {
case "log_level": case "log_level":
if (value != null) { if (value != null) {
mPreferences.setLogLevel(mContext, Integer.parseInt(value)); mPreferences.setLogLevel(mContext, Integer.parseInt(value), true);
} }
break; break;
default: default:

View File

@@ -18,6 +18,7 @@ public class TermuxAPIAppSharedPreferences {
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPreferences; private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;
private static final String LOG_TAG = "TermuxAPIAppSharedPreferences"; private static final String LOG_TAG = "TermuxAPIAppSharedPreferences";
@@ -25,6 +26,7 @@ public class TermuxAPIAppSharedPreferences {
private TermuxAPIAppSharedPreferences(@Nonnull Context context) { private TermuxAPIAppSharedPreferences(@Nonnull Context context) {
mContext = context; mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext); mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
} }
/** /**
@@ -65,15 +67,23 @@ public class TermuxAPIAppSharedPreferences {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION); return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
} }
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
public int getLogLevel() { return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
} }
public void setLogLevel(Context context, int logLevel) {
public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel); logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, false); SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
} }
} }

View File

@@ -18,6 +18,7 @@ public class TermuxBootAppSharedPreferences {
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPreferences; private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;
private static final String LOG_TAG = "TermuxBootAppSharedPreferences"; private static final String LOG_TAG = "TermuxBootAppSharedPreferences";
@@ -25,6 +26,7 @@ public class TermuxBootAppSharedPreferences {
private TermuxBootAppSharedPreferences(@Nonnull Context context) { private TermuxBootAppSharedPreferences(@Nonnull Context context) {
mContext = context; mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext); mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
} }
/** /**
@@ -65,15 +67,23 @@ public class TermuxBootAppSharedPreferences {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_BOOT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION); return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_BOOT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
} }
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
public int getLogLevel() { return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_BOOT_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
} }
public void setLogLevel(Context context, int logLevel) {
public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel); logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, logLevel, false); SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_BOOT_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
} }
} }

View File

@@ -18,6 +18,7 @@ public class TermuxStylingAppSharedPreferences {
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPreferences; private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;
private static final String LOG_TAG = "TermuxStylingAppSharedPreferences"; private static final String LOG_TAG = "TermuxStylingAppSharedPreferences";
@@ -25,6 +26,7 @@ public class TermuxStylingAppSharedPreferences {
private TermuxStylingAppSharedPreferences(@Nonnull Context context) { private TermuxStylingAppSharedPreferences(@Nonnull Context context) {
mContext = context; mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext); mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
} }
/** /**
@@ -65,15 +67,23 @@ public class TermuxStylingAppSharedPreferences {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_STYLING_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION); return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_STYLING_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
} }
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
public int getLogLevel() { return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_STYLING_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
} }
public void setLogLevel(Context context, int logLevel) {
public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel); logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, logLevel, false); SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_STYLING_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
} }
} }

View File

@@ -18,6 +18,7 @@ public class TermuxWidgetAppSharedPreferences {
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPreferences; private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;
private static final String LOG_TAG = "TermuxWidgetAppSharedPreferences"; private static final String LOG_TAG = "TermuxWidgetAppSharedPreferences";
@@ -25,6 +26,7 @@ public class TermuxWidgetAppSharedPreferences {
private TermuxWidgetAppSharedPreferences(@Nonnull Context context) { private TermuxWidgetAppSharedPreferences(@Nonnull Context context) {
mContext = context; mContext = context;
mSharedPreferences = getPrivateSharedPreferences(mContext); mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
} }
/** /**
@@ -65,15 +67,23 @@ public class TermuxWidgetAppSharedPreferences {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_WIDGET_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION); return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_WIDGET_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
} }
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
if (context == null) return null;
public int getLogLevel() { return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_WIDGET_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
} }
public void setLogLevel(Context context, int logLevel) {
public int getLogLevel(boolean readFromFile) {
if (readFromFile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}
public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel); logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, logLevel, false); SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_WIDGET_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
} }
} }