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:
@@ -129,10 +129,16 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
*/
|
||||
private boolean mIsVisible;
|
||||
|
||||
/**
|
||||
* The {@link TermuxActivity} is in an invalid state and must not be run.
|
||||
*/
|
||||
private boolean mIsInvalidState;
|
||||
|
||||
private int mNavBarHeight;
|
||||
|
||||
private int mTerminalToolbarDefaultHeight;
|
||||
|
||||
|
||||
private static final int CONTEXT_MENU_SELECT_URL_ID = 0;
|
||||
private static final int CONTEXT_MENU_SHARE_TRANSCRIPT_ID = 1;
|
||||
private static final int CONTEXT_MENU_AUTOFILL_ID = 2;
|
||||
@@ -159,8 +165,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
// notification with the crash details if it did
|
||||
CrashUtils.notifyCrash(this, LOG_TAG);
|
||||
|
||||
// Load termux shared preferences and properties
|
||||
mPreferences = new TermuxAppSharedPreferences(this);
|
||||
// Load termux shared properties
|
||||
mProperties = new TermuxAppSharedProperties(this);
|
||||
|
||||
setActivityTheme();
|
||||
@@ -169,6 +174,15 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
|
||||
setContentView(R.layout.activity_termux);
|
||||
|
||||
// Load termux shared preferences
|
||||
// This will also fail if TermuxConstants.TERMUX_PACKAGE_NAME does not equal applicationId
|
||||
mPreferences = TermuxAppSharedPreferences.build(this, true);
|
||||
if (mPreferences == null) {
|
||||
// An AlertDialog should have shown to kill the app, so we don't continue running activity code
|
||||
mIsInvalidState = true;
|
||||
return;
|
||||
}
|
||||
|
||||
View content = findViewById(android.R.id.content);
|
||||
content.setOnApplyWindowInsetsListener((v, insets) -> {
|
||||
mNavBarHeight = insets.getSystemWindowInsetBottom();
|
||||
@@ -211,6 +225,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
|
||||
Logger.logDebug(LOG_TAG, "onStart");
|
||||
|
||||
if (mIsInvalidState) return;
|
||||
|
||||
mIsVisible = true;
|
||||
|
||||
if (mTermuxService != null) {
|
||||
@@ -236,6 +252,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
Logger.logVerbose(LOG_TAG, "onResume");
|
||||
|
||||
if (mIsInvalidState) return;
|
||||
|
||||
mTermuxTerminalViewClient.setSoftKeyboardState(true, false);
|
||||
}
|
||||
|
||||
@@ -302,6 +322,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
|
||||
Logger.logDebug(LOG_TAG, "onStop");
|
||||
|
||||
if (mIsInvalidState) return;
|
||||
|
||||
mIsVisible = false;
|
||||
|
||||
// Store current session in shared preferences so that it can be restored later in
|
||||
@@ -318,12 +340,19 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
|
||||
Logger.logDebug(LOG_TAG, "onDestroy");
|
||||
|
||||
if (mIsInvalidState) return;
|
||||
|
||||
if (mTermuxService != null) {
|
||||
// Do not leave service and session clients with references to activity.
|
||||
mTermuxService.unsetTermuxTerminalSessionClient();
|
||||
mTermuxService = null;
|
||||
}
|
||||
|
||||
try {
|
||||
unbindService(this);
|
||||
} catch (Exception e) {
|
||||
// ignore.
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -20,7 +20,8 @@ public class TermuxApplication extends Application {
|
||||
|
||||
private void setLogLevel() {
|
||||
// Load the log level from shared preferences and set it to the {@link Logger.CURRENT_LOG_LEVEL}
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(getApplicationContext());
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(getApplicationContext());
|
||||
if (preferences == null) return;
|
||||
preferences.setLogLevel(null, preferences.getLogLevel());
|
||||
Logger.logDebug("Starting Application");
|
||||
}
|
||||
|
@@ -743,8 +743,9 @@ public final class TermuxService extends Service implements TermuxTask.TermuxTas
|
||||
|
||||
private void setCurrentStoredTerminalSession(TerminalSession session) {
|
||||
if (session == null) return;
|
||||
// Make the newly created session the current one to be displayed:
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(this);
|
||||
// Make the newly created session the current one to be displayed
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(this);
|
||||
if (preferences == null) return;
|
||||
preferences.setCurrentSession(session.mHandle);
|
||||
}
|
||||
|
||||
|
@@ -33,12 +33,12 @@ class TermuxPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
private TermuxPreferencesDataStore(Context context) {
|
||||
mContext = context;
|
||||
mPreferences = new TermuxAppSharedPreferences(context);
|
||||
mPreferences = TermuxAppSharedPreferences.build(context, true);
|
||||
}
|
||||
|
||||
public static synchronized TermuxPreferencesDataStore getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new TermuxPreferencesDataStore(context.getApplicationContext());
|
||||
mInstance = new TermuxPreferencesDataStore(context);
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
@@ -28,7 +28,7 @@ public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {
|
||||
PreferenceCategory loggingCategory = findPreference("logging");
|
||||
|
||||
if (loggingCategory != null) {
|
||||
final ListPreference logLevelListPreference = setLogLevelListPreferenceData(findPreference("log_level"), getActivity());
|
||||
final ListPreference logLevelListPreference = setLogLevelListPreferenceData(findPreference("log_level"), getContext());
|
||||
loggingCategory.addPreference(logLevelListPreference);
|
||||
}
|
||||
}
|
||||
@@ -60,12 +60,12 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
private DebuggingPreferencesDataStore(Context context) {
|
||||
mContext = context;
|
||||
mPreferences = new TermuxAppSharedPreferences(context);
|
||||
mPreferences = TermuxAppSharedPreferences.build(context, true);
|
||||
}
|
||||
|
||||
public static synchronized DebuggingPreferencesDataStore getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DebuggingPreferencesDataStore(context.getApplicationContext());
|
||||
mInstance = new DebuggingPreferencesDataStore(context);
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
@@ -75,6 +75,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
|
||||
@Override
|
||||
@Nullable
|
||||
public String getString(String key, @Nullable String defValue) {
|
||||
if (mPreferences == null) return null;
|
||||
if (key == null) return null;
|
||||
|
||||
switch (key) {
|
||||
@@ -87,6 +88,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
@Override
|
||||
public void putString(String key, @Nullable String value) {
|
||||
if (mPreferences == null) return;
|
||||
if (key == null) return;
|
||||
|
||||
switch (key) {
|
||||
@@ -104,6 +106,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value) {
|
||||
if (mPreferences == null) return;
|
||||
if (key == null) return;
|
||||
|
||||
switch (key) {
|
||||
@@ -123,6 +126,7 @@ class DebuggingPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defValue) {
|
||||
if (mPreferences == null) return false;
|
||||
switch (key) {
|
||||
case "terminal_view_key_logging_enabled":
|
||||
return mPreferences.isTerminalViewKeyLoggingEnabled();
|
||||
|
@@ -33,12 +33,12 @@ class TerminalIOPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
private TerminalIOPreferencesDataStore(Context context) {
|
||||
mContext = context;
|
||||
mPreferences = new TermuxAppSharedPreferences(context);
|
||||
mPreferences = TermuxAppSharedPreferences.build(context, true);
|
||||
}
|
||||
|
||||
public static synchronized TerminalIOPreferencesDataStore getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new TerminalIOPreferencesDataStore(context.getApplicationContext());
|
||||
mInstance = new TerminalIOPreferencesDataStore(context);
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
@@ -47,6 +47,7 @@ class TerminalIOPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value) {
|
||||
if (mPreferences == null) return;
|
||||
if (key == null) return;
|
||||
|
||||
switch (key) {
|
||||
@@ -63,6 +64,8 @@ class TerminalIOPreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defValue) {
|
||||
if (mPreferences == null) return false;
|
||||
|
||||
switch (key) {
|
||||
case "soft_keyboard_enabled":
|
||||
return mPreferences.isSoftKeyboardEnabled();
|
||||
|
@@ -47,7 +47,9 @@ public class CrashUtils {
|
||||
if (context == null) return;
|
||||
|
||||
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context);
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
||||
if (preferences == null) return;
|
||||
|
||||
// If user has disabled notifications for crashes
|
||||
if (!preferences.areCrashReportNotificationsEnabled())
|
||||
return;
|
||||
|
@@ -139,7 +139,9 @@ public class PluginUtils {
|
||||
}
|
||||
|
||||
|
||||
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context);
|
||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
||||
if (preferences == null) return;
|
||||
|
||||
// If user has disabled notifications for plugin, then just return
|
||||
if (!preferences.arePluginErrorNotificationsEnabled() && !forceNotification)
|
||||
return;
|
||||
|
@@ -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