diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index 766f941f45..7bf806be1e 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -229,23 +229,13 @@ public void onStart() { mIsVisible = true; - if (mTermuxService != null) { - // The service has connected, but data may have changed since we were last in the foreground. - // Get the session stored in shared preferences stored by {@link #onStop} if its valid, - // otherwise get the last session currently running. - mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast()); - termuxSessionListNotifyUpdated(); - } + if (mTermuxTerminalSessionClient != null) + mTermuxTerminalSessionClient.onStart(); - registerTermuxActivityBroadcastReceiver(); + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onStart(); - // If user changed the preference from {@link TermuxSettings} activity and returns, then - // update the {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value. - mTerminalView.setIsTerminalViewKeyLoggingEnabled(mPreferences.isTerminalViewKeyLoggingEnabled()); - - // The current terminal session may have changed while being away, force - // a refresh of the displayed terminal. - mTerminalView.onScreenUpdated(); + registerTermuxActivityBroadcastReceiver(); } @Override @@ -256,12 +246,64 @@ public void onResume() { if (mIsInvalidState) return; - mTermuxTerminalViewClient.setSoftKeyboardState(true, false); + if (mTermuxTerminalSessionClient != null) + mTermuxTerminalSessionClient.onResume(); + + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onResume(); + } + + @Override + protected void onStop() { + super.onStop(); + + Logger.logDebug(LOG_TAG, "onStop"); + + if (mIsInvalidState) return; + + mIsVisible = false; + + if (mTermuxTerminalSessionClient != null) + mTermuxTerminalSessionClient.onStop(); + + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onStop(); + + unregisterTermuxActivityBroadcastReceiever(); + getDrawer().closeDrawers(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + 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. + } + } - // Start terminal cursor blinking if enabled - mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true); + @Override + public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { + super.onSaveInstanceState(savedInstanceState); + saveTerminalToolbarTextInput(savedInstanceState); } + + + + /** * Part of the {@link ServiceConnection} interface. The service is bound with * {@link #bindService(Intent, ServiceConnection, int)} in {@link #onCreate(Bundle)} which will cause a call to this @@ -319,53 +361,7 @@ public void onServiceDisconnected(ComponentName name) { finishActivityIfNotFinishing(); } - @Override - protected void onStop() { - super.onStop(); - - Logger.logDebug(LOG_TAG, "onStop"); - - if (mIsInvalidState) return; - - mIsVisible = false; - - // Store current session in shared preferences so that it can be restored later in - // {@link #onStart} if needed. - mTermuxTerminalSessionClient.setCurrentStoredSession(); - - // Stop terminal cursor blinking if enabled - mTermuxTerminalViewClient.setTerminalCursorBlinkerState(false); - unregisterTermuxActivityBroadcastReceiever(); - getDrawer().closeDrawers(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - - 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 - public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { - super.onSaveInstanceState(savedInstanceState); - saveTerminalToolbarTextInput(savedInstanceState); - } @@ -386,6 +382,32 @@ private void setDrawerTheme() { + private void setTermuxTerminalViewAndClients() { + // Set termux terminal view and session clients + mTermuxTerminalSessionClient = new TermuxTerminalSessionClient(this); + mTermuxTerminalViewClient = new TermuxTerminalViewClient(this, mTermuxTerminalSessionClient); + + // Set termux terminal view + mTerminalView = findViewById(R.id.terminal_view); + mTerminalView.setTerminalViewClient(mTermuxTerminalViewClient); + + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onCreate(); + + if (mTermuxTerminalSessionClient != null) + mTermuxTerminalSessionClient.onCreate(); + } + + private void setTermuxSessionsListView() { + ListView termuxSessionsListView = findViewById(R.id.terminal_sessions_list); + mTermuxSessionListViewController = new TermuxSessionsListViewController(this, mTermuxService.getTermuxSessions()); + termuxSessionsListView.setAdapter(mTermuxSessionListViewController); + termuxSessionsListView.setOnItemClickListener(mTermuxSessionListViewController); + termuxSessionsListView.setOnItemLongClickListener(mTermuxSessionListViewController); + } + + + private void setTerminalToolbarView(Bundle savedInstanceState) { final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager); if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE); @@ -466,36 +488,6 @@ private void setToggleKeyboardView() { - private void setTermuxTerminalViewAndClients() { - // Set termux terminal view and session clients - mTermuxTerminalSessionClient = new TermuxTerminalSessionClient(this); - mTermuxTerminalViewClient = new TermuxTerminalViewClient(this, mTermuxTerminalSessionClient); - - // Set termux terminal view - mTerminalView = findViewById(R.id.terminal_view); - mTerminalView.setTerminalViewClient(mTermuxTerminalViewClient); - - mTerminalView.setTextSize(mPreferences.getFontSize()); - mTerminalView.setKeepScreenOn(mPreferences.shouldKeepScreenOn()); - - // Set {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value - mTerminalView.setIsTerminalViewKeyLoggingEnabled(mPreferences.isTerminalViewKeyLoggingEnabled()); - - mTermuxTerminalSessionClient.checkForFontAndColors(); - } - - private void setTermuxSessionsListView() { - ListView termuxSessionsListView = findViewById(R.id.terminal_sessions_list); - mTermuxSessionListViewController = new TermuxSessionsListViewController(this, mTermuxService.getTermuxSessions()); - termuxSessionsListView.setAdapter(mTermuxSessionListViewController); - termuxSessionsListView.setOnItemClickListener(mTermuxSessionListViewController); - termuxSessionsListView.setOnItemLongClickListener(mTermuxSessionListViewController); - } - - - - - @SuppressLint("RtlHardcoded") @Override public void onBackPressed() { @@ -780,7 +772,7 @@ public void onReceive(Context context, Intent intent) { return; case TERMUX_ACTIVITY.ACTION_RELOAD_STYLE: Logger.logDebug(LOG_TAG, "Received intent to reload styling"); - reloadTermuxActivityStyling(); + reloadActivityStyling(); return; default: } @@ -788,11 +780,7 @@ public void onReceive(Context context, Intent intent) { } } - private void reloadTermuxActivityStyling() { - if (mTermuxTerminalSessionClient != null) { - mTermuxTerminalSessionClient.checkForFontAndColors(); - } - + private void reloadActivityStyling() { if (mProperties!= null) { mProperties.loadTermuxPropertiesFromDisk(); @@ -803,10 +791,11 @@ private void reloadTermuxActivityStyling() { setTerminalToolbarHeight(); - mTermuxTerminalViewClient.setSoftKeyboardState(false, true); - - mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true); + if (mTermuxTerminalSessionClient != null) + mTermuxTerminalSessionClient.onReload(); + if (mTermuxTerminalViewClient != null) + mTermuxTerminalViewClient.onReload(); // To change the activity and drawer theme, activity needs to be recreated. // But this will destroy the activity, and will call the onCreate() again. diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java index 7033ac5084..4e836aef38 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java @@ -47,6 +47,31 @@ public TermuxTerminalSessionClient(TermuxActivity activity) { this.mActivity = activity; } + /** + * Should be called when mActivity.onCreate() is called + */ + public void onCreate() { + // Set terminal fonts and colors + checkForFontAndColors(); + } + + /** + * Should be called when mActivity.onStart() is called + */ + public void onStart() { + // The service has connected, but data may have changed since we were last in the foreground. + // Get the session stored in shared preferences stored by {@link #onStop} if its valid, + // otherwise get the last session currently running. + if (mActivity.getTermuxService() != null) { + setCurrentSession(getCurrentStoredSessionOrLast()); + termuxSessionListNotifyUpdated(); + } + + // The current terminal session may have changed while being away, force + // a refresh of the displayed terminal. + mActivity.getTerminalView().onScreenUpdated(); + } + /** * Should be called when mActivity.onResume() is called */ @@ -61,6 +86,10 @@ public void onResume() { * Should be called when mActivity.onStop() is called */ public void onStop() { + // Store current session in shared preferences so that it can be restored later in + // {@link #onStart} if needed. + setCurrentStoredSession(); + // Release mBellSoundPool resources, specially to prevent exceptions like the following to be thrown // java.util.concurrent.TimeoutException: android.media.SoundPool.finalize() timed out after 10 seconds // Bell is not played in background anyways @@ -68,6 +97,14 @@ public void onStop() { releaseBellSoundPool(); } + /** + * Should be called when mActivity.reloadActivityStyling() is called + */ + public void onReload() { + // Set terminal fonts and colors + checkForFontAndColors(); + } + @Override diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index 246181199c..83ea528f5d 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -63,6 +63,56 @@ public TermuxTerminalViewClient(TermuxActivity activity, TermuxTerminalSessionCl this.mTermuxTerminalSessionClient = termuxTerminalSessionClient; } + /** + * Should be called when mActivity.onCreate() is called + */ + public void onCreate() { + mActivity.getTerminalView().setTextSize(mActivity.getPreferences().getFontSize()); + mActivity.getTerminalView().setKeepScreenOn(mActivity.getPreferences().shouldKeepScreenOn()); + } + + /** + * Should be called when mActivity.onStart() is called + */ + public void onStart() { + + // Set {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value + // Also required if user changed the preference from {@link TermuxSettings} activity and returns + mActivity.getTerminalView().setIsTerminalViewKeyLoggingEnabled(mActivity.getPreferences().isTerminalViewKeyLoggingEnabled()); + } + + /** + * Should be called when mActivity.onResume() is called + */ + public void onResume() { + // Show the soft keyboard if required + setSoftKeyboardState(true, false); + + // Start terminal cursor blinking if enabled + setTerminalCursorBlinkerState(true); + } + + /** + * Should be called when mActivity.onStop() is called + */ + public void onStop() { + // Stop terminal cursor blinking if enabled + setTerminalCursorBlinkerState(false); + } + + /** + * Should be called when mActivity.reloadActivityStyling() is called + */ + public void onReload() { + // Show the soft keyboard if required + setSoftKeyboardState(false, true); + + // Start terminal cursor blinking if enabled + setTerminalCursorBlinkerState(true); + } + + + @Override public float onScale(float scale) { if (scale < 0.9f || scale > 1.1f) {