Skip to content

Commit

Permalink
Implement TermuxActivity callbacks in TermuxTerminalViewClient and Te…
Browse files Browse the repository at this point in the history
…rmuxTerminalSessionClient
  • Loading branch information
agnostic-apollo committed May 16, 2021
1 parent 58d5770 commit d736b1e
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 103 deletions.
195 changes: 92 additions & 103 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}



Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -780,19 +772,15 @@ 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:
}
}
}
}

private void reloadTermuxActivityStyling() {
if (mTermuxTerminalSessionClient != null) {
mTermuxTerminalSessionClient.checkForFontAndColors();
}

private void reloadActivityStyling() {
if (mProperties!= null) {
mProperties.loadTermuxPropertiesFromDisk();

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -61,13 +86,25 @@ 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
// Related: https://stackoverflow.com/a/28708351/14686958
releaseBellSoundPool();
}

/**
* Should be called when mActivity.reloadActivityStyling() is called
*/
public void onReload() {
// Set terminal fonts and colors
checkForFontAndColors();
}



@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d736b1e

Please sign in to comment.