Skip to content

Commit d736b1e

Browse files
Implement TermuxActivity callbacks in TermuxTerminalViewClient and TermuxTerminalSessionClient
1 parent 58d5770 commit d736b1e

File tree

3 files changed

+179
-103
lines changed

3 files changed

+179
-103
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

Lines changed: 92 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,13 @@ public void onStart() {
229229

230230
mIsVisible = true;
231231

232-
if (mTermuxService != null) {
233-
// The service has connected, but data may have changed since we were last in the foreground.
234-
// Get the session stored in shared preferences stored by {@link #onStop} if its valid,
235-
// otherwise get the last session currently running.
236-
mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast());
237-
termuxSessionListNotifyUpdated();
238-
}
232+
if (mTermuxTerminalSessionClient != null)
233+
mTermuxTerminalSessionClient.onStart();
239234

240-
registerTermuxActivityBroadcastReceiver();
235+
if (mTermuxTerminalViewClient != null)
236+
mTermuxTerminalViewClient.onStart();
241237

242-
// If user changed the preference from {@link TermuxSettings} activity and returns, then
243-
// update the {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value.
244-
mTerminalView.setIsTerminalViewKeyLoggingEnabled(mPreferences.isTerminalViewKeyLoggingEnabled());
245-
246-
// The current terminal session may have changed while being away, force
247-
// a refresh of the displayed terminal.
248-
mTerminalView.onScreenUpdated();
238+
registerTermuxActivityBroadcastReceiver();
249239
}
250240

251241
@Override
@@ -256,12 +246,64 @@ public void onResume() {
256246

257247
if (mIsInvalidState) return;
258248

259-
mTermuxTerminalViewClient.setSoftKeyboardState(true, false);
249+
if (mTermuxTerminalSessionClient != null)
250+
mTermuxTerminalSessionClient.onResume();
251+
252+
if (mTermuxTerminalViewClient != null)
253+
mTermuxTerminalViewClient.onResume();
254+
}
255+
256+
@Override
257+
protected void onStop() {
258+
super.onStop();
259+
260+
Logger.logDebug(LOG_TAG, "onStop");
261+
262+
if (mIsInvalidState) return;
263+
264+
mIsVisible = false;
265+
266+
if (mTermuxTerminalSessionClient != null)
267+
mTermuxTerminalSessionClient.onStop();
268+
269+
if (mTermuxTerminalViewClient != null)
270+
mTermuxTerminalViewClient.onStop();
271+
272+
unregisterTermuxActivityBroadcastReceiever();
273+
getDrawer().closeDrawers();
274+
}
275+
276+
@Override
277+
public void onDestroy() {
278+
super.onDestroy();
279+
280+
Logger.logDebug(LOG_TAG, "onDestroy");
281+
282+
if (mIsInvalidState) return;
283+
284+
if (mTermuxService != null) {
285+
// Do not leave service and session clients with references to activity.
286+
mTermuxService.unsetTermuxTerminalSessionClient();
287+
mTermuxService = null;
288+
}
289+
290+
try {
291+
unbindService(this);
292+
} catch (Exception e) {
293+
// ignore.
294+
}
295+
}
260296

261-
// Start terminal cursor blinking if enabled
262-
mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true);
297+
@Override
298+
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
299+
super.onSaveInstanceState(savedInstanceState);
300+
saveTerminalToolbarTextInput(savedInstanceState);
263301
}
264302

303+
304+
305+
306+
265307
/**
266308
* Part of the {@link ServiceConnection} interface. The service is bound with
267309
* {@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) {
319361
finishActivityIfNotFinishing();
320362
}
321363

322-
@Override
323-
protected void onStop() {
324-
super.onStop();
325-
326-
Logger.logDebug(LOG_TAG, "onStop");
327-
328-
if (mIsInvalidState) return;
329-
330-
mIsVisible = false;
331-
332-
// Store current session in shared preferences so that it can be restored later in
333-
// {@link #onStart} if needed.
334-
mTermuxTerminalSessionClient.setCurrentStoredSession();
335-
336-
// Stop terminal cursor blinking if enabled
337-
mTermuxTerminalViewClient.setTerminalCursorBlinkerState(false);
338364

339-
unregisterTermuxActivityBroadcastReceiever();
340-
getDrawer().closeDrawers();
341-
}
342-
343-
@Override
344-
public void onDestroy() {
345-
super.onDestroy();
346-
347-
Logger.logDebug(LOG_TAG, "onDestroy");
348-
349-
if (mIsInvalidState) return;
350-
351-
if (mTermuxService != null) {
352-
// Do not leave service and session clients with references to activity.
353-
mTermuxService.unsetTermuxTerminalSessionClient();
354-
mTermuxService = null;
355-
}
356-
357-
try {
358-
unbindService(this);
359-
} catch (Exception e) {
360-
// ignore.
361-
}
362-
}
363-
364-
@Override
365-
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
366-
super.onSaveInstanceState(savedInstanceState);
367-
saveTerminalToolbarTextInput(savedInstanceState);
368-
}
369365

370366

371367

@@ -386,6 +382,32 @@ private void setDrawerTheme() {
386382

387383

388384

385+
private void setTermuxTerminalViewAndClients() {
386+
// Set termux terminal view and session clients
387+
mTermuxTerminalSessionClient = new TermuxTerminalSessionClient(this);
388+
mTermuxTerminalViewClient = new TermuxTerminalViewClient(this, mTermuxTerminalSessionClient);
389+
390+
// Set termux terminal view
391+
mTerminalView = findViewById(R.id.terminal_view);
392+
mTerminalView.setTerminalViewClient(mTermuxTerminalViewClient);
393+
394+
if (mTermuxTerminalViewClient != null)
395+
mTermuxTerminalViewClient.onCreate();
396+
397+
if (mTermuxTerminalSessionClient != null)
398+
mTermuxTerminalSessionClient.onCreate();
399+
}
400+
401+
private void setTermuxSessionsListView() {
402+
ListView termuxSessionsListView = findViewById(R.id.terminal_sessions_list);
403+
mTermuxSessionListViewController = new TermuxSessionsListViewController(this, mTermuxService.getTermuxSessions());
404+
termuxSessionsListView.setAdapter(mTermuxSessionListViewController);
405+
termuxSessionsListView.setOnItemClickListener(mTermuxSessionListViewController);
406+
termuxSessionsListView.setOnItemLongClickListener(mTermuxSessionListViewController);
407+
}
408+
409+
410+
389411
private void setTerminalToolbarView(Bundle savedInstanceState) {
390412
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
391413
if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE);
@@ -466,36 +488,6 @@ private void setToggleKeyboardView() {
466488

467489

468490

469-
private void setTermuxTerminalViewAndClients() {
470-
// Set termux terminal view and session clients
471-
mTermuxTerminalSessionClient = new TermuxTerminalSessionClient(this);
472-
mTermuxTerminalViewClient = new TermuxTerminalViewClient(this, mTermuxTerminalSessionClient);
473-
474-
// Set termux terminal view
475-
mTerminalView = findViewById(R.id.terminal_view);
476-
mTerminalView.setTerminalViewClient(mTermuxTerminalViewClient);
477-
478-
mTerminalView.setTextSize(mPreferences.getFontSize());
479-
mTerminalView.setKeepScreenOn(mPreferences.shouldKeepScreenOn());
480-
481-
// Set {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value
482-
mTerminalView.setIsTerminalViewKeyLoggingEnabled(mPreferences.isTerminalViewKeyLoggingEnabled());
483-
484-
mTermuxTerminalSessionClient.checkForFontAndColors();
485-
}
486-
487-
private void setTermuxSessionsListView() {
488-
ListView termuxSessionsListView = findViewById(R.id.terminal_sessions_list);
489-
mTermuxSessionListViewController = new TermuxSessionsListViewController(this, mTermuxService.getTermuxSessions());
490-
termuxSessionsListView.setAdapter(mTermuxSessionListViewController);
491-
termuxSessionsListView.setOnItemClickListener(mTermuxSessionListViewController);
492-
termuxSessionsListView.setOnItemLongClickListener(mTermuxSessionListViewController);
493-
}
494-
495-
496-
497-
498-
499491
@SuppressLint("RtlHardcoded")
500492
@Override
501493
public void onBackPressed() {
@@ -780,19 +772,15 @@ public void onReceive(Context context, Intent intent) {
780772
return;
781773
case TERMUX_ACTIVITY.ACTION_RELOAD_STYLE:
782774
Logger.logDebug(LOG_TAG, "Received intent to reload styling");
783-
reloadTermuxActivityStyling();
775+
reloadActivityStyling();
784776
return;
785777
default:
786778
}
787779
}
788780
}
789781
}
790782

791-
private void reloadTermuxActivityStyling() {
792-
if (mTermuxTerminalSessionClient != null) {
793-
mTermuxTerminalSessionClient.checkForFontAndColors();
794-
}
795-
783+
private void reloadActivityStyling() {
796784
if (mProperties!= null) {
797785
mProperties.loadTermuxPropertiesFromDisk();
798786

@@ -803,10 +791,11 @@ private void reloadTermuxActivityStyling() {
803791

804792
setTerminalToolbarHeight();
805793

806-
mTermuxTerminalViewClient.setSoftKeyboardState(false, true);
807-
808-
mTermuxTerminalViewClient.setTerminalCursorBlinkerState(true);
794+
if (mTermuxTerminalSessionClient != null)
795+
mTermuxTerminalSessionClient.onReload();
809796

797+
if (mTermuxTerminalViewClient != null)
798+
mTermuxTerminalViewClient.onReload();
810799

811800
// To change the activity and drawer theme, activity needs to be recreated.
812801
// But this will destroy the activity, and will call the onCreate() again.

app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ public TermuxTerminalSessionClient(TermuxActivity activity) {
4747
this.mActivity = activity;
4848
}
4949

50+
/**
51+
* Should be called when mActivity.onCreate() is called
52+
*/
53+
public void onCreate() {
54+
// Set terminal fonts and colors
55+
checkForFontAndColors();
56+
}
57+
58+
/**
59+
* Should be called when mActivity.onStart() is called
60+
*/
61+
public void onStart() {
62+
// The service has connected, but data may have changed since we were last in the foreground.
63+
// Get the session stored in shared preferences stored by {@link #onStop} if its valid,
64+
// otherwise get the last session currently running.
65+
if (mActivity.getTermuxService() != null) {
66+
setCurrentSession(getCurrentStoredSessionOrLast());
67+
termuxSessionListNotifyUpdated();
68+
}
69+
70+
// The current terminal session may have changed while being away, force
71+
// a refresh of the displayed terminal.
72+
mActivity.getTerminalView().onScreenUpdated();
73+
}
74+
5075
/**
5176
* Should be called when mActivity.onResume() is called
5277
*/
@@ -61,13 +86,25 @@ public void onResume() {
6186
* Should be called when mActivity.onStop() is called
6287
*/
6388
public void onStop() {
89+
// Store current session in shared preferences so that it can be restored later in
90+
// {@link #onStart} if needed.
91+
setCurrentStoredSession();
92+
6493
// Release mBellSoundPool resources, specially to prevent exceptions like the following to be thrown
6594
// java.util.concurrent.TimeoutException: android.media.SoundPool.finalize() timed out after 10 seconds
6695
// Bell is not played in background anyways
6796
// Related: https://stackoverflow.com/a/28708351/14686958
6897
releaseBellSoundPool();
6998
}
7099

100+
/**
101+
* Should be called when mActivity.reloadActivityStyling() is called
102+
*/
103+
public void onReload() {
104+
// Set terminal fonts and colors
105+
checkForFontAndColors();
106+
}
107+
71108

72109

73110
@Override

app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,56 @@ public TermuxTerminalViewClient(TermuxActivity activity, TermuxTerminalSessionCl
6363
this.mTermuxTerminalSessionClient = termuxTerminalSessionClient;
6464
}
6565

66+
/**
67+
* Should be called when mActivity.onCreate() is called
68+
*/
69+
public void onCreate() {
70+
mActivity.getTerminalView().setTextSize(mActivity.getPreferences().getFontSize());
71+
mActivity.getTerminalView().setKeepScreenOn(mActivity.getPreferences().shouldKeepScreenOn());
72+
}
73+
74+
/**
75+
* Should be called when mActivity.onStart() is called
76+
*/
77+
public void onStart() {
78+
79+
// Set {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value
80+
// Also required if user changed the preference from {@link TermuxSettings} activity and returns
81+
mActivity.getTerminalView().setIsTerminalViewKeyLoggingEnabled(mActivity.getPreferences().isTerminalViewKeyLoggingEnabled());
82+
}
83+
84+
/**
85+
* Should be called when mActivity.onResume() is called
86+
*/
87+
public void onResume() {
88+
// Show the soft keyboard if required
89+
setSoftKeyboardState(true, false);
90+
91+
// Start terminal cursor blinking if enabled
92+
setTerminalCursorBlinkerState(true);
93+
}
94+
95+
/**
96+
* Should be called when mActivity.onStop() is called
97+
*/
98+
public void onStop() {
99+
// Stop terminal cursor blinking if enabled
100+
setTerminalCursorBlinkerState(false);
101+
}
102+
103+
/**
104+
* Should be called when mActivity.reloadActivityStyling() is called
105+
*/
106+
public void onReload() {
107+
// Show the soft keyboard if required
108+
setSoftKeyboardState(false, true);
109+
110+
// Start terminal cursor blinking if enabled
111+
setTerminalCursorBlinkerState(true);
112+
}
113+
114+
115+
66116
@Override
67117
public float onScale(float scale) {
68118
if (scale < 0.9f || scale > 1.1f) {

0 commit comments

Comments
 (0)