2021-10-19 12:26:39 +00:00
|
|
|
package com.termux.shared.termux.settings.preferences;
|
2021-04-06 12:23:12 +00:00
|
|
|
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
import android.app.Activity;
|
2021-04-06 12:23:12 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2021-09-04 22:37:07 +00:00
|
|
|
import androidx.annotation.Nullable;
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
|
|
|
|
import com.termux.shared.packages.PackageUtils;
|
2021-10-19 12:26:39 +00:00
|
|
|
import com.termux.shared.settings.preferences.SharedPreferenceUtils;
|
2021-04-07 06:31:30 +00:00
|
|
|
import com.termux.shared.termux.TermuxConstants;
|
2021-10-19 12:26:39 +00:00
|
|
|
import com.termux.shared.termux.settings.preferences.TermuxPreferenceConstants.TERMUX_TASKER_APP;
|
2021-04-07 06:31:30 +00:00
|
|
|
import com.termux.shared.logger.Logger;
|
2021-04-06 12:23:12 +00:00
|
|
|
|
|
|
|
public class TermuxTaskerAppSharedPreferences {
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final SharedPreferences mSharedPreferences;
|
|
|
|
private final SharedPreferences mMultiProcessSharedPreferences;
|
|
|
|
|
|
|
|
|
|
|
|
private static final String LOG_TAG = "TermuxTaskerAppSharedPreferences";
|
|
|
|
|
2021-09-04 22:37:07 +00:00
|
|
|
private TermuxTaskerAppSharedPreferences(@NonNull Context context) {
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
mContext = context;
|
2021-04-06 12:23:12 +00:00
|
|
|
mSharedPreferences = getPrivateSharedPreferences(mContext);
|
|
|
|
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(mContext);
|
|
|
|
}
|
|
|
|
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
/**
|
|
|
|
* 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_TASKER_PACKAGE_NAME}.
|
|
|
|
* @return Returns the {@link TermuxTaskerAppSharedPreferences}. This will {@code null} if an exception is raised.
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public static TermuxTaskerAppSharedPreferences build(@NonNull final Context context) {
|
|
|
|
Context termuxTaskerPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_TASKER_PACKAGE_NAME);
|
|
|
|
if (termuxTaskerPackageContext == null)
|
|
|
|
return null;
|
|
|
|
else
|
|
|
|
return new TermuxTaskerAppSharedPreferences(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_TASKER_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.
|
2021-08-27 19:29:53 +00:00
|
|
|
* @return Returns the {@link TermuxTaskerAppSharedPreferences}. This will {@code null} if an exception is raised.
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
*/
|
|
|
|
public static TermuxTaskerAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) {
|
|
|
|
Context termuxTaskerPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_TASKER_PACKAGE_NAME, exitAppOnError);
|
|
|
|
if (termuxTaskerPackageContext == null)
|
|
|
|
return null;
|
|
|
|
else
|
|
|
|
return new TermuxTaskerAppSharedPreferences(termuxTaskerPackageContext);
|
|
|
|
}
|
|
|
|
|
2021-04-06 12:23:12 +00:00
|
|
|
private static SharedPreferences getPrivateSharedPreferences(Context context) {
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
if (context == null) return null;
|
2021-04-06 12:23:12 +00:00
|
|
|
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
|
Ensure we read/write to/from current SharedPreferences
When getting SharedPreferences of other termux sharedUserId app packages, we get its Context first and if its null, it would mean that the package is not installed or likely has a different signature. For this case, we force exit the app in some places, since that shouldn't occur. Previously, if it was null, we were defaulting to getting SharedPreferences of current package context instead, which would mix keys of other packages with current one. SharedPreferences of other app packages aren't being used currently, so this isn't an issue, this commit just fixes the issue for future.
Force exit will also be triggered if Termux is forked and TermuxConstants.TERMUX_PACKAGE_NAME is not updated to the same value as applicationId since TermuxActivity.onCreate() will fail to get SharedPreferences of TermuxConstants.TERMUX_PACKAGE_NAME.
Moreover, its normally not allowed to install apps with different signatures, but if its done, we "may" need AndroidManifest `queries` entries in andorid 11, check PackageUtils.getSigningCertificateSHA256DigestForPackage() for details.
2021-05-13 22:54:13 +00:00
|
|
|
if (context == null) return null;
|
2021-04-06 12:23:12 +00:00
|
|
|
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-02 01:20:39 +00:00
|
|
|
public int getLogLevel(boolean readFromFile) {
|
|
|
|
if (readFromFile)
|
2021-04-06 12:23:12 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-05 09:52:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
public int getLastPendingIntentRequestCode() {
|
|
|
|
return SharedPreferenceUtils.getInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LAST_PENDING_INTENT_REQUEST_CODE, TERMUX_TASKER_APP.DEFAULT_VALUE_KEY_LAST_PENDING_INTENT_REQUEST_CODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLastPendingIntentRequestCode(int lastPendingIntentRequestCode) {
|
|
|
|
SharedPreferenceUtils.setInt(mSharedPreferences, TERMUX_TASKER_APP.KEY_LAST_PENDING_INTENT_REQUEST_CODE, lastPendingIntentRequestCode, false);
|
|
|
|
}
|
|
|
|
|
2021-04-06 12:23:12 +00:00
|
|
|
}
|