Skip to content

Commit

Permalink
Add TermuxTaskerAppSharedPreferences to handle termux-tasker shared p…
Browse files Browse the repository at this point in the history
…references
  • Loading branch information
agnostic-apollo committed Apr 6, 2021
1 parent 9cee710 commit c9a476c
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.termux.app.settings.preferences;

import android.content.Context;
import android.content.SharedPreferences;

import com.termux.app.TermuxConstants;
import com.termux.app.settings.preferences.TermuxPreferenceConstants.TERMUX_TASKER_APP;
import com.termux.app.utils.DataUtils;
import com.termux.app.utils.Logger;
import com.termux.app.utils.TermuxUtils;

import javax.annotation.Nonnull;

public class TermuxTaskerAppSharedPreferences {

private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final SharedPreferences mMultiProcessSharedPreferences;


private static final String LOG_TAG = "TermuxTaskerAppSharedPreferences";

public TermuxTaskerAppSharedPreferences(@Nonnull Context context) {
// We use the default context if failed to get termux-tasker package context
mContext = DataUtils.getDefaultIfNull(TermuxUtils.getTermuxTaskerPackageContext(context), context);
mSharedPreferences = getPrivateSharedPreferences(mContext);
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
}

private static SharedPreferences getPrivateSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}

private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
}



public int getLogLevel(boolean readFromFfile) {
if(readFromFfile)
return SharedPreferenceUtils.getInt(mMultiProcessSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
else
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, Logger.DEFAULT_LOG_LEVEL);
}

public void setLogLevel(Context context, int logLevel, boolean commitToFile) {
logLevel = Logger.setLogLevel(context, logLevel);
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LOG_LEVEL, logLevel, commitToFile);
}

}

0 comments on commit c9a476c

Please sign in to comment.