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:
agnostic-apollo
2021-05-14 03:54:13 +05:00
parent af115c9966
commit 79df863b75
17 changed files with 218 additions and 36 deletions

View File

@@ -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

View File

@@ -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");
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;