-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Add SharedPrefernces controllers for all current published ter…
…mux 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`.
- Loading branch information
1 parent
5a8c4f1
commit 582e569
Showing
14 changed files
with
677 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/termux/app/fragments/settings/TermuxAPIPreferencesFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.TermuxAPIAppSharedPreferences; | ||
|
||
@Keep | ||
public class TermuxAPIPreferencesFragment extends PreferenceFragmentCompat { | ||
|
||
@Override | ||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | ||
Context context = getContext(); | ||
if (context == null) return; | ||
|
||
PreferenceManager preferenceManager = getPreferenceManager(); | ||
preferenceManager.setPreferenceDataStore(TermuxAPIPreferencesDataStore.getInstance(context)); | ||
|
||
setPreferencesFromResource(R.xml.termux_api_preferences, rootKey); | ||
} | ||
|
||
} | ||
|
||
class TermuxAPIPreferencesDataStore extends PreferenceDataStore { | ||
|
||
private final Context mContext; | ||
private final TermuxAPIAppSharedPreferences mPreferences; | ||
|
||
private static TermuxAPIPreferencesDataStore mInstance; | ||
|
||
private TermuxAPIPreferencesDataStore(Context context) { | ||
mContext = context; | ||
mPreferences = TermuxAPIAppSharedPreferences.build(context, true); | ||
} | ||
|
||
public static synchronized TermuxAPIPreferencesDataStore getInstance(Context context) { | ||
if (mInstance == null) { | ||
mInstance = new TermuxAPIPreferencesDataStore(context); | ||
} | ||
return mInstance; | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
.../main/java/com/termux/app/fragments/settings/termux_api/DebuggingPreferencesFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.termux.app.fragments.settings.termux_api; | ||
|
||
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.TermuxAPIAppSharedPreferences; | ||
|
||
@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_api_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) { | ||
TermuxAPIAppSharedPreferences preferences = TermuxAPIAppSharedPreferences.build(context, true); | ||
if (preferences == null) return; | ||
|
||
com.termux.app.fragments.settings.termux.DebuggingPreferencesFragment. | ||
setLogLevelListPreferenceData(logLevelListPreference, context, preferences.getLogLevel()); | ||
loggingCategory.addPreference(logLevelListPreference); | ||
} | ||
} | ||
} | ||
|
||
class DebuggingPreferencesDataStore extends PreferenceDataStore { | ||
|
||
private final Context mContext; | ||
private final TermuxAPIAppSharedPreferences mPreferences; | ||
|
||
private static DebuggingPreferencesDataStore mInstance; | ||
|
||
private DebuggingPreferencesDataStore(Context context) { | ||
mContext = context; | ||
mPreferences = TermuxAPIAppSharedPreferences.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()); | ||
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)); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_api.DebuggingPreferencesFragment"/> | ||
|
||
</PreferenceScreen> |
79 changes: 79 additions & 0 deletions
79
...d/src/main/java/com/termux/shared/settings/preferences/TermuxAPIAppSharedPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.termux.shared.settings.preferences; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.termux.shared.logger.Logger; | ||
import com.termux.shared.packages.PackageUtils; | ||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_API_APP; | ||
import com.termux.shared.termux.TermuxConstants; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class TermuxAPIAppSharedPreferences { | ||
|
||
private final Context mContext; | ||
private final SharedPreferences mSharedPreferences; | ||
|
||
|
||
private static final String LOG_TAG = "TermuxAPIAppSharedPreferences"; | ||
|
||
private TermuxAPIAppSharedPreferences(@Nonnull Context context) { | ||
mContext = context; | ||
mSharedPreferences = getPrivateSharedPreferences(mContext); | ||
} | ||
|
||
/** | ||
* Get the {@link Context} for a package name. | ||
* | ||
* @param context The {@link Context} to use to get the {@link Context} of the | ||
* {@link TermuxConstants#TERMUX_API_PACKAGE_NAME}. | ||
* @return Returns the {@link TermuxAPIAppSharedPreferences}. This will {@code null} if an exception is raised. | ||
*/ | ||
@Nullable | ||
public static TermuxAPIAppSharedPreferences build(@NonNull final Context context) { | ||
Context termuxTaskerPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_API_PACKAGE_NAME); | ||
if (termuxTaskerPackageContext == null) | ||
return null; | ||
else | ||
return new TermuxAPIAppSharedPreferences(termuxTaskerPackageContext); | ||
} | ||
|
||
/** | ||
* Get the {@link Context} for a package name. | ||
* | ||
* @param context The {@link Activity} to use to get the {@link Context} of the | ||
* {@link TermuxConstants#TERMUX_API_PACKAGE_NAME}. | ||
* @param exitAppOnError If {@code true} and failed to get package context, then a dialog will | ||
* be shown which when dismissed will exit the app. | ||
* @return Returns the {@link TermuxAPIAppSharedPreferences}. This will {@code null} if an exception is raised. | ||
*/ | ||
public static TermuxAPIAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) { | ||
Context termuxTaskerPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_API_PACKAGE_NAME, exitAppOnError); | ||
if (termuxTaskerPackageContext == null) | ||
return null; | ||
else | ||
return new TermuxAPIAppSharedPreferences(termuxTaskerPackageContext); | ||
} | ||
|
||
private static SharedPreferences getPrivateSharedPreferences(Context context) { | ||
if (context == null) return null; | ||
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_API_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION); | ||
} | ||
|
||
|
||
|
||
public int getLogLevel() { | ||
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL); | ||
} | ||
|
||
public void setLogLevel(Context context, int logLevel) { | ||
logLevel = Logger.setLogLevel(context, logLevel); | ||
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_API_APP.KEY_LOG_LEVEL, logLevel, false); | ||
} | ||
|
||
} |
Oops, something went wrong.