mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08: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.
This commit is contained in:
@@ -75,6 +75,15 @@ public final class DialogUtils {
|
||||
dialogHolder[0].show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a message in a dialog
|
||||
*
|
||||
* @param context The {@link Context} to use to start the dialog. An {@link Activity} {@link Context}
|
||||
* must be passed, otherwise exceptions will be thrown.
|
||||
* @param titleText The title text of the dialog.
|
||||
* @param messageText The message text of the dialog.
|
||||
* @param onDismiss The {@link DialogInterface.OnDismissListener} to run when dialog is dismissed.
|
||||
*/
|
||||
public static void showMessage(Context context, String titleText, String messageText, final DialogInterface.OnDismissListener onDismiss) {
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light_Dialog)
|
||||
|
@@ -61,7 +61,9 @@ public class NotificationUtils {
|
||||
public synchronized static int getNextNotificationId(final Context context) {
|
||||
if (context == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
||||
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context);
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
||||
if (preferences == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
||||
|
||||
int lastNotificationId = preferences.getLastNotificationId();
|
||||
|
||||
int nextNotificationId = lastNotificationId + 1;
|
||||
|
@@ -7,28 +7,62 @@ import android.content.pm.PackageManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.R;
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.interact.DialogUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.termux.TermuxConstants;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class PackageUtils {
|
||||
|
||||
private static final String LOG_TAG = "PackageUtils";
|
||||
|
||||
/**
|
||||
* Get the {@link Context} for the package name.
|
||||
*
|
||||
* @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}.
|
||||
* @param packageName The package name whose {@link Context} to get.
|
||||
* @return Returns the {@link Context}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static Context getContextForPackage(@NonNull final Context context, String packageName) {
|
||||
try {
|
||||
return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
|
||||
} catch (Exception e) {
|
||||
Logger.logStackTraceWithMessage("Failed to get \"" + packageName + "\" package context.", e);
|
||||
Logger.logVerbose(LOG_TAG, "Failed to get \"" + packageName + "\" package context: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link Context} for a package name.
|
||||
*
|
||||
* @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}.
|
||||
* @param packageName The package name whose {@link Context} to get.
|
||||
* @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 Context}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static Context getContextForPackageOrExitApp(@NonNull Context context, String packageName, final boolean exitAppOnError) {
|
||||
Context packageContext = getContextForPackage(context, packageName);
|
||||
|
||||
if (packageContext == null && exitAppOnError) {
|
||||
String errorMessage = context.getString(R.string.error_get_package_context_failed_message,
|
||||
packageName, TermuxConstants.TERMUX_GITHUB_REPO_URL);
|
||||
Logger.logError(LOG_TAG, errorMessage);
|
||||
DialogUtils.exitAppWithErrorMessage(context,
|
||||
context.getString(R.string.error_get_package_context_failed_title),
|
||||
errorMessage);
|
||||
}
|
||||
|
||||
return packageContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link PackageInfo} for the package associated with the {@code context}.
|
||||
*
|
||||
@@ -46,6 +80,7 @@ public class PackageUtils {
|
||||
* @param flags The flags to pass to {@link PackageManager#getPackageInfo(String, int)}.
|
||||
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context, final int flags) {
|
||||
try {
|
||||
return context.getPackageManager().getPackageInfo(context.getPackageName(), flags);
|
||||
@@ -100,6 +135,7 @@ public class PackageUtils {
|
||||
* @param context The {@link Context} for the package.
|
||||
* @return Returns the {@code versionCode}. This will be {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static Integer getVersionCodeForPackage(@NonNull final Context context) {
|
||||
try {
|
||||
return getPackageInfoForPackage(context).versionCode;
|
||||
@@ -114,6 +150,7 @@ public class PackageUtils {
|
||||
* @param context The {@link Context} for the package.
|
||||
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static String getVersionNameForPackage(@NonNull final Context context) {
|
||||
try {
|
||||
return getPackageInfoForPackage(context).versionName;
|
||||
@@ -122,15 +159,23 @@ public class PackageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the {@code SHA-256 digest} of signing certificate for the package associated with the {@code context}.
|
||||
*
|
||||
* @param context The {@link Context} for the package.
|
||||
* @return Returns the{@code SHA-256 digest}. This will be {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static String getSigningCertificateSHA256DigestForPackage(@NonNull final Context context) {
|
||||
try {
|
||||
/*
|
||||
* Todo: We may need AndroidManifest queries entries if package is installed but with a different signature on android 11
|
||||
* https://developer.android.com/training/package-visibility
|
||||
* Need a device that allows (manual) installation of apk with mismatched signature of
|
||||
* sharedUserId apps to test. Currently, if its done, PackageManager just doesn't load
|
||||
* the package and removes its apk automatically if its installed as a user app instead of system app
|
||||
* W/PackageManager: Failed to parse /path/to/com.termux.tasker.apk: Signature mismatch for shared user: SharedUserSetting{xxxxxxx com.termux/10xxx}
|
||||
*/
|
||||
PackageInfo packageInfo = getPackageInfoForPackage(context, PackageManager.GET_SIGNATURES);
|
||||
if (packageInfo == null) return null;
|
||||
return DataUtils.bytesToHex(MessageDigest.getInstance("SHA-256").digest(packageInfo.signatures[0].toByteArray()));
|
||||
|
@@ -75,7 +75,9 @@ public class PermissionUtils {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return true;
|
||||
|
||||
if (!PermissionUtils.checkDisplayOverOtherAppsPermission(context)) {
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context);
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
||||
if (preferences == null) return false;
|
||||
|
||||
if (preferences.arePluginErrorNotificationsEnabled())
|
||||
Logger.showToast(context, context.getString(R.string.error_display_over_other_apps_permission_not_granted), true);
|
||||
return false;
|
||||
|
@@ -1,16 +1,20 @@
|
||||
package com.termux.shared.settings.preferences;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.packages.PackageUtils;
|
||||
import com.termux.shared.termux.TermuxConstants;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.termux.TermuxUtils;
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_APP;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class TermuxAppSharedPreferences {
|
||||
|
||||
@@ -24,15 +28,48 @@ public class TermuxAppSharedPreferences {
|
||||
|
||||
private static final String LOG_TAG = "TermuxAppSharedPreferences";
|
||||
|
||||
public TermuxAppSharedPreferences(@Nonnull Context context) {
|
||||
// We use the default context if failed to get termux package context
|
||||
mContext = DataUtils.getDefaultIfNull(TermuxUtils.getTermuxPackageContext(context), context);
|
||||
private TermuxAppSharedPreferences(@Nonnull Context context) {
|
||||
mContext = context;
|
||||
mSharedPreferences = getPrivateSharedPreferences(mContext);
|
||||
|
||||
setFontVariables(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_PACKAGE_NAME}.
|
||||
* @return Returns the {@link TermuxAppSharedPreferences}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
@Nullable
|
||||
public static TermuxAppSharedPreferences build(@NonNull final Context context) {
|
||||
Context termuxPackageContext = PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_PACKAGE_NAME);
|
||||
if (termuxPackageContext == null)
|
||||
return null;
|
||||
else
|
||||
return new TermuxAppSharedPreferences(termuxPackageContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_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 TermuxAppSharedPreferences}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
public static TermuxAppSharedPreferences build(@NonNull final Context context, final boolean exitAppOnError) {
|
||||
Context termuxPackageContext = PackageUtils.getContextForPackageOrExitApp(context, TermuxConstants.TERMUX_PACKAGE_NAME, exitAppOnError);
|
||||
if (termuxPackageContext == null)
|
||||
return null;
|
||||
else
|
||||
return new TermuxAppSharedPreferences(termuxPackageContext);
|
||||
}
|
||||
|
||||
private static SharedPreferences getPrivateSharedPreferences(Context context) {
|
||||
if (context == null) return null;
|
||||
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,18 @@
|
||||
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.packages.PackageUtils;
|
||||
import com.termux.shared.termux.TermuxConstants;
|
||||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants.TERMUX_TASKER_APP;
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.termux.TermuxUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class TermuxTaskerAppSharedPreferences {
|
||||
|
||||
@@ -20,18 +23,52 @@ public class TermuxTaskerAppSharedPreferences {
|
||||
|
||||
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);
|
||||
private TermuxTaskerAppSharedPreferences(@Nonnull Context context) {
|
||||
mContext = context;
|
||||
mSharedPreferences = getPrivateSharedPreferences(mContext);
|
||||
mMultiProcessSharedPreferences = getPrivateAndMultiProcessSharedPreferences(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_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.
|
||||
* @return Returns the {@link TermuxAppSharedPreferences}. This will {@code null} if an exception is raised.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
private static SharedPreferences getPrivateSharedPreferences(Context context) {
|
||||
if (context == null) return null;
|
||||
return SharedPreferenceUtils.getPrivateSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
||||
}
|
||||
|
||||
private static SharedPreferences getPrivateAndMultiProcessSharedPreferences(Context context) {
|
||||
if (context == null) return null;
|
||||
return SharedPreferenceUtils.getPrivateAndMultiProcessSharedPreferences(context, TermuxConstants.TERMUX_TASKER_DEFAULT_PREFERENCES_FILE_BASENAME_WITHOUT_EXTENSION);
|
||||
}
|
||||
|
||||
|
@@ -228,7 +228,6 @@ public class TermuxUtils {
|
||||
appendPropertyToMarkdown(markdownString,"IS_DEBUG_BUILD", PackageUtils.isAppForPackageADebugBuild(context));
|
||||
|
||||
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(context);
|
||||
Logger.logError("'" + signingCertificateSHA256Digest + "'");
|
||||
if (signingCertificateSHA256Digest != null) {
|
||||
appendPropertyToMarkdown(markdownString,"APK_RELEASE", getAPKRelease(signingCertificateSHA256Digest));
|
||||
appendPropertyToMarkdown(markdownString,"SIGNING_CERTIFICATE_SHA256_DIGEST", signingCertificateSHA256Digest);
|
||||
@@ -381,7 +380,7 @@ public class TermuxUtils {
|
||||
*/
|
||||
public static String geAPTInfoMarkdownString(@NonNull final Context context) {
|
||||
|
||||
String aptInfoScript = null;
|
||||
String aptInfoScript;
|
||||
InputStream inputStream = context.getResources().openRawResource(com.termux.shared.R.raw.apt_info_script);
|
||||
try {
|
||||
aptInfoScript = IOUtils.toString(inputStream, Charset.defaultCharset());
|
||||
|
@@ -35,8 +35,10 @@
|
||||
android:id="@+id/dialog_message"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:autoLink="web|email"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@android:color/tab_indicator_text"/>
|
||||
android:textColor="@android:color/tab_indicator_text"
|
||||
android:textColorLink="@android:color/black"/>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
|
@@ -61,6 +61,13 @@
|
||||
<string name="error_file_not_executable">The %1$s at path is not executable. Permission Denied.</string>
|
||||
|
||||
|
||||
|
||||
<!-- PackageUtils -->
|
||||
<string name="error_get_package_context_failed_title">Failed To Get Package Context</string>
|
||||
<string name="error_get_package_context_failed_message">Failed to get package context for the \"%1$s\" package. This may be because the app package is not installed or it has different APK signature from the current app. Check install instruction at %2$s for more details.</string>
|
||||
|
||||
|
||||
|
||||
<!-- PermissionUtils -->
|
||||
<string name="message_sudo_please_grant_permissions">Please grant permissions on next screen</string>
|
||||
<string name="error_display_over_other_apps_permission_not_granted">&TERMUX_APP_NAME; requires \"Display over other apps\" permission to start terminal sessions from background on Android >= 10. Grants it from Settings -> Apps -> &TERMUX_APP_NAME; -> Advanced</string>
|
||||
@@ -72,11 +79,6 @@
|
||||
|
||||
|
||||
|
||||
<!-- TermuxUtils -->
|
||||
<string name="msg_report_issue">If you want to report this issue, then copy its text from the options menu (3-dots on top right) and post an issue on one of the following links.\n\nIf you are posting a Termux app crash report, then please provide details on what you were doing that caused the crash and how to reproduce it, if possible.\n\nIf you are posting an issue on Github, then post it in the repository at which the report belongs at. You may optionally remove any device specific info that you consider private or don\'t want to share or that is not relevant to the issue.</string>
|
||||
|
||||
|
||||
|
||||
<!-- ShellUtils -->
|
||||
<string name="error_sending_sigkill_to_process">Sending SIGKILL to process on user request or because android is killing the execution service</string>
|
||||
<string name="error_execution_cancelled">Execution has been cancelled since execution service is being killed</string>
|
||||
@@ -87,6 +89,11 @@
|
||||
|
||||
|
||||
|
||||
<!-- TermuxUtils -->
|
||||
<string name="msg_report_issue">If you want to report this issue, then copy its text from the options menu (3-dots on top right) and post an issue on one of the following links.\n\nIf you are posting a Termux app crash report, then please provide details on what you were doing that caused the crash and how to reproduce it, if possible.\n\nIf you are posting an issue on Github, then post it in the repository at which the report belongs at. You may optionally remove any device specific info that you consider private or don\'t want to share or that is not relevant to the issue.</string>
|
||||
|
||||
|
||||
|
||||
<!-- Log Level -->
|
||||
<string name="log_level_title">Log Level</string>
|
||||
<string name="log_level_off">"Off"</string>
|
||||
|
Reference in New Issue
Block a user