Skip to content

Commit

Permalink
Fixed: Fix terminal sessions being re-added if "New Session" shortcut…
Browse files Browse the repository at this point in the history
… or termux-reload-settings was used

If TermuxActivity was recreated then the original intent was re-delivered, resulting in a new session being re-added each time.

Closes termux#2566
  • Loading branch information
agnostic-apollo committed Feb 5, 2022
1 parent 2a1c5a7 commit a56ed57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
*/
private boolean isOnResumeAfterOnCreate = false;

/**
* If activity was restarted like due to call to {@link #recreate()} after receiving
* {@link TERMUX_ACTIVITY#ACTION_RELOAD_STYLE}, system dark night mode was changed or activity
* was killed by android.
*/
private boolean mIsActivityRecreated = false;

/**
* The {@link TermuxActivity} is in an invalid state and must not be run.
*/
Expand All @@ -175,6 +182,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
private static final int CONTEXT_MENU_REPORT_ID = 9;

private static final String ARG_TERMINAL_TOOLBAR_TEXT_INPUT = "terminal_toolbar_text_input";
private static final String ARG_ACTIVITY_RECREATED = "activity_recreated";

private static final String LOG_TAG = "TermuxActivity";

Expand All @@ -183,6 +191,9 @@ public void onCreate(Bundle savedInstanceState) {
Logger.logDebug(LOG_TAG, "onCreate");
isOnResumeAfterOnCreate = true;

if (savedInstanceState != null)
mIsActivityRecreated = savedInstanceState.getBoolean(ARG_ACTIVITY_RECREATED, false);

// Check if a crash happened on last run of the app and show a
// notification with the crash details if it did
CrashUtils.notifyAppCrashOnLastRun(this, LOG_TAG);
Expand Down Expand Up @@ -339,6 +350,7 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {

super.onSaveInstanceState(savedInstanceState);
saveTerminalToolbarTextInput(savedInstanceState);
savedInstanceState.putBoolean(ARG_ACTIVITY_RECREATED, true);
}


Expand All @@ -359,15 +371,17 @@ public void onServiceConnected(ComponentName componentName, IBinder service) {

setTermuxSessionsListView();

final Intent intent = getIntent();
setIntent(null);

if (mTermuxService.isTermuxSessionsEmpty()) {
if (mIsVisible) {
TermuxInstaller.setupBootstrapIfNeeded(TermuxActivity.this, () -> {
if (mTermuxService == null) return; // Activity might have been destroyed.
try {
Bundle bundle = getIntent().getExtras();
boolean launchFailsafe = false;
if (bundle != null) {
launchFailsafe = bundle.getBoolean(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
if (intent != null && intent.getExtras() != null) {
launchFailsafe = intent.getExtras().getBoolean(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
}
mTermuxTerminalSessionClient.addNewSession(launchFailsafe, null);
} catch (WindowManager.BadTokenException e) {
Expand All @@ -379,10 +393,12 @@ public void onServiceConnected(ComponentName componentName, IBinder service) {
finishActivityIfNotFinishing();
}
} else {
Intent i = getIntent();
if (i != null && Intent.ACTION_RUN.equals(i.getAction())) {
// If termux was started from launcher "New session" shortcut and activity is recreated,
// then the original intent will be re-delivered, resulting in a new session being re-added
// each time.
if (!mIsActivityRecreated && intent != null && Intent.ACTION_RUN.equals(intent.getAction())) {
// Android 7.1 app shortcut from res/xml/shortcuts.xml.
boolean isFailSafe = i.getBooleanExtra(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
boolean isFailSafe = intent.getBooleanExtra(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
mTermuxTerminalSessionClient.addNewSession(isFailSafe, null);
} else {
mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast());
Expand Down Expand Up @@ -541,7 +557,7 @@ private void setToggleKeyboardView() {
});

findViewById(R.id.toggle_keyboard_button).setOnLongClickListener(v -> {
toggleTerminalToolbar();
//toggleTerminalToolbar();
return true;
});
}
Expand Down Expand Up @@ -808,6 +824,10 @@ public boolean isOnResumeAfterOnCreate() {
return isOnResumeAfterOnCreate;
}

public boolean isActivityRecreated() {
return mIsActivityRecreated;
}



public TermuxService getTermuxService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onStart() {
*/
public void onResume() {
// Show the soft keyboard if required
setSoftKeyboardState(true, false);
setSoftKeyboardState(true, mActivity.isActivityRecreated());

mTerminalCursorBlinkerStateAlreadySet = false;

Expand Down

0 comments on commit a56ed57

Please sign in to comment.