Implement TermuxActivity callbacks in TermuxTerminalViewClient and TermuxTerminalSessionClient

This commit is contained in:
agnostic-apollo
2021-05-16 20:45:44 +05:00
parent 58d577066a
commit d736b1eba5
3 changed files with 179 additions and 103 deletions

View File

@@ -229,23 +229,13 @@ public final class TermuxActivity extends Activity implements ServiceConnection
mIsVisible = true; mIsVisible = true;
if (mTermuxService != null) { if (mTermuxTerminalSessionClient != null)
// The service has connected, but data may have changed since we were last in the foreground. mTermuxTerminalSessionClient.onStart();
// Get the session stored in shared preferences stored by {@link #onStop} if its valid,
// otherwise get the last session currently running. if (mTermuxTerminalViewClient != null)
mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast()); mTermuxTerminalViewClient.onStart();
termuxSessionListNotifyUpdated();
}
registerTermuxActivityBroadcastReceiver(); registerTermuxActivityBroadcastReceiver();
// 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();
} }
@Override @Override
@@ -256,12 +246,64 @@ public final class TermuxActivity extends Activity implements ServiceConnection
if (mIsInvalidState) return; if (mIsInvalidState) return;
mTermuxTerminalViewClient.setSoftKeyboardState(true, false); if (mTermuxTerminalSessionClient != null)
mTermuxTerminalSessionClient.onResume();
// Start terminal cursor blinking if enabled if (mTermuxTerminalViewClient != null)
mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true); 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.
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
saveTerminalToolbarTextInput(savedInstanceState);
}
/** /**
* Part of the {@link ServiceConnection} interface. The service is bound with * 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 * {@link #bindService(Intent, ServiceConnection, int)} in {@link #onCreate(Bundle)} which will cause a call to this
@@ -319,53 +361,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
finishActivityIfNotFinishing(); 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 @@ public final class TermuxActivity extends Activity implements ServiceConnection
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) { private void setTerminalToolbarView(Bundle savedInstanceState) {
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager); final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE); if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE);
@@ -466,36 +488,6 @@ public final class TermuxActivity extends Activity implements ServiceConnection
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") @SuppressLint("RtlHardcoded")
@Override @Override
public void onBackPressed() { public void onBackPressed() {
@@ -780,7 +772,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
return; return;
case TERMUX_ACTIVITY.ACTION_RELOAD_STYLE: case TERMUX_ACTIVITY.ACTION_RELOAD_STYLE:
Logger.logDebug(LOG_TAG, "Received intent to reload styling"); Logger.logDebug(LOG_TAG, "Received intent to reload styling");
reloadTermuxActivityStyling(); reloadActivityStyling();
return; return;
default: default:
} }
@@ -788,11 +780,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
} }
} }
private void reloadTermuxActivityStyling() { private void reloadActivityStyling() {
if (mTermuxTerminalSessionClient != null) {
mTermuxTerminalSessionClient.checkForFontAndColors();
}
if (mProperties!= null) { if (mProperties!= null) {
mProperties.loadTermuxPropertiesFromDisk(); mProperties.loadTermuxPropertiesFromDisk();
@@ -803,10 +791,11 @@ public final class TermuxActivity extends Activity implements ServiceConnection
setTerminalToolbarHeight(); setTerminalToolbarHeight();
mTermuxTerminalViewClient.setSoftKeyboardState(false, true); if (mTermuxTerminalSessionClient != null)
mTermuxTerminalSessionClient.onReload();
mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true);
if (mTermuxTerminalViewClient != null)
mTermuxTerminalViewClient.onReload();
// To change the activity and drawer theme, activity needs to be recreated. // To change the activity and drawer theme, activity needs to be recreated.
// But this will destroy the activity, and will call the onCreate() again. // But this will destroy the activity, and will call the onCreate() again.

View File

@@ -47,6 +47,31 @@ public class TermuxTerminalSessionClient extends TermuxTerminalSessionClientBase
this.mActivity = 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 * Should be called when mActivity.onResume() is called
*/ */
@@ -61,6 +86,10 @@ public class TermuxTerminalSessionClient extends TermuxTerminalSessionClientBase
* Should be called when mActivity.onStop() is called * Should be called when mActivity.onStop() is called
*/ */
public void onStop() { 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 // 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 // java.util.concurrent.TimeoutException: android.media.SoundPool.finalize() timed out after 10 seconds
// Bell is not played in background anyways // Bell is not played in background anyways
@@ -68,6 +97,14 @@ public class TermuxTerminalSessionClient extends TermuxTerminalSessionClientBase
releaseBellSoundPool(); releaseBellSoundPool();
} }
/**
* Should be called when mActivity.reloadActivityStyling() is called
*/
public void onReload() {
// Set terminal fonts and colors
checkForFontAndColors();
}
@Override @Override

View File

@@ -63,6 +63,56 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase {
this.mTermuxTerminalSessionClient = termuxTerminalSessionClient; 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 @Override
public float onScale(float scale) { public float onScale(float scale) {
if (scale < 0.9f || scale > 1.1f) { if (scale < 0.9f || scale > 1.1f) {