diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java
index 9cf396a8..582d1415 100644
--- a/app/src/main/java/com/termux/app/TermuxActivity.java
+++ b/app/src/main/java/com/termux/app/TermuxActivity.java
@@ -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;
}
- unbindService(this);
+
+ try {
+ unbindService(this);
+ } catch (Exception e) {
+ // ignore.
+ }
}
@Override
diff --git a/app/src/main/java/com/termux/app/TermuxApplication.java b/app/src/main/java/com/termux/app/TermuxApplication.java
index 3bc6689c..beb66634 100644
--- a/app/src/main/java/com/termux/app/TermuxApplication.java
+++ b/app/src/main/java/com/termux/app/TermuxApplication.java
@@ -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");
}
diff --git a/app/src/main/java/com/termux/app/TermuxService.java b/app/src/main/java/com/termux/app/TermuxService.java
index 39c92023..3457b7d8 100644
--- a/app/src/main/java/com/termux/app/TermuxService.java
+++ b/app/src/main/java/com/termux/app/TermuxService.java
@@ -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);
}
diff --git a/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java
index 67543f63..b92472c6 100644
--- a/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java
+++ b/app/src/main/java/com/termux/app/fragments/settings/TermuxPreferencesFragment.java
@@ -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;
}
diff --git a/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java
index 279cdc65..dd5795eb 100644
--- a/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java
+++ b/app/src/main/java/com/termux/app/fragments/settings/termux/DebuggingPreferencesFragment.java
@@ -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();
diff --git a/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java b/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java
index 2e750c85..109daf95 100644
--- a/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java
+++ b/app/src/main/java/com/termux/app/fragments/settings/termux/TerminalIOPreferencesFragment.java
@@ -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();
diff --git a/app/src/main/java/com/termux/app/utils/CrashUtils.java b/app/src/main/java/com/termux/app/utils/CrashUtils.java
index f2d6011b..670277b7 100644
--- a/app/src/main/java/com/termux/app/utils/CrashUtils.java
+++ b/app/src/main/java/com/termux/app/utils/CrashUtils.java
@@ -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;
diff --git a/app/src/main/java/com/termux/app/utils/PluginUtils.java b/app/src/main/java/com/termux/app/utils/PluginUtils.java
index 72d4af53..d1b7dba9 100644
--- a/app/src/main/java/com/termux/app/utils/PluginUtils.java
+++ b/app/src/main/java/com/termux/app/utils/PluginUtils.java
@@ -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;
diff --git a/termux-shared/src/main/java/com/termux/shared/interact/DialogUtils.java b/termux-shared/src/main/java/com/termux/shared/interact/DialogUtils.java
index d6e81741..e70c4dcb 100644
--- a/termux-shared/src/main/java/com/termux/shared/interact/DialogUtils.java
+++ b/termux-shared/src/main/java/com/termux/shared/interact/DialogUtils.java
@@ -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)
diff --git a/termux-shared/src/main/java/com/termux/shared/notification/NotificationUtils.java b/termux-shared/src/main/java/com/termux/shared/notification/NotificationUtils.java
index fe9d87e6..b31072e8 100644
--- a/termux-shared/src/main/java/com/termux/shared/notification/NotificationUtils.java
+++ b/termux-shared/src/main/java/com/termux/shared/notification/NotificationUtils.java
@@ -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;
diff --git a/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java b/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java
index b1192e7e..054aa5c8 100644
--- a/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java
+++ b/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java
@@ -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()));
diff --git a/termux-shared/src/main/java/com/termux/shared/packages/PermissionUtils.java b/termux-shared/src/main/java/com/termux/shared/packages/PermissionUtils.java
index 65c0f73d..e6a8569c 100644
--- a/termux-shared/src/main/java/com/termux/shared/packages/PermissionUtils.java
+++ b/termux-shared/src/main/java/com/termux/shared/packages/PermissionUtils.java
@@ -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;
diff --git a/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxAppSharedPreferences.java b/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxAppSharedPreferences.java
index 4e66c8f5..c2979ee0 100644
--- a/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxAppSharedPreferences.java
+++ b/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxAppSharedPreferences.java
@@ -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);
}
diff --git a/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxTaskerAppSharedPreferences.java b/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxTaskerAppSharedPreferences.java
index b2f0ba78..412c9b22 100644
--- a/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxTaskerAppSharedPreferences.java
+++ b/termux-shared/src/main/java/com/termux/shared/settings/preferences/TermuxTaskerAppSharedPreferences.java
@@ -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);
}
diff --git a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java
index 706f556b..6ad9a18e 100644
--- a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java
+++ b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java
@@ -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());
diff --git a/termux-shared/src/main/res/layout/dialog_show_message.xml b/termux-shared/src/main/res/layout/dialog_show_message.xml
index f074d08c..f5dd82f1 100644
--- a/termux-shared/src/main/res/layout/dialog_show_message.xml
+++ b/termux-shared/src/main/res/layout/dialog_show_message.xml
@@ -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"/>
diff --git a/termux-shared/src/main/res/values/strings.xml b/termux-shared/src/main/res/values/strings.xml
index 71bd7bca..d9eedf33 100644
--- a/termux-shared/src/main/res/values/strings.xml
+++ b/termux-shared/src/main/res/values/strings.xml
@@ -61,6 +61,13 @@
The %1$s at path is not executable. Permission Denied.
+
+
+ Failed To Get Package Context
+ 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.
+
+
+
Please grant permissions on next screen
&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
@@ -72,11 +79,6 @@
-
- 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.
-
-
-
Sending SIGKILL to process on user request or because android is killing the execution service
Execution has been cancelled since execution service is being killed
@@ -87,6 +89,11 @@
+
+ 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.
+
+
+
Log Level
"Off"