Skip to content

Commit e5c5174

Browse files
Fix TermuxActivityBroadcastReceiver wrongly designed intent actions and extras
From now on - TERMUX_ACTIVITY.ACTION_REQUEST_PERMISSIONS should be used for requesting storage permissions. - TERMUX_ACTIVITY.ACTION_RELOAD_STYLE should be used for reloading styling. - TERMUX_ACTIVITY.EXTRA_RELOAD_STYLE which was previously used for requesting storage permissions if its value equaled "storage" has been deprecated. If more actions need to be supported in future, add them to TermuxActivity.registerTermuxActivityBroadcastReceiver() IntentFilter.
1 parent f1034c2 commit e5c5174

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

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

+62-30
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
117117
/**
118118
* The {@link TermuxActivity} broadcast receiver for various things like terminal style configuration changes.
119119
*/
120-
private final BroadcastReceiver mTermuxActivityBroadcastReceiever = new TermuxActivityBroadcastReceiver();
120+
private final BroadcastReceiver mTermuxActivityBroadcastReceiver = new TermuxActivityBroadcastReceiver();
121121

122122
/**
123123
* The last toast shown, used cancel current toast before showing new in {@link #showToast(String, boolean)}.
@@ -222,7 +222,7 @@ public void onStart() {
222222
termuxSessionListNotifyUpdated();
223223
}
224224

225-
registerReceiver(mTermuxActivityBroadcastReceiever, new IntentFilter(TERMUX_ACTIVITY.ACTION_RELOAD_STYLE));
225+
registerTermuxActivityBroadcastReceiver();
226226

227227
// If user changed the preference from {@link TermuxSettings} activity and returns, then
228228
// update the {@link TerminalView#TERMINAL_VIEW_KEY_LOGGING_ENABLED} value.
@@ -309,7 +309,7 @@ protected void onStop() {
309309
// {@link #onStart} if needed.
310310
mTermuxTerminalSessionClient.setCurrentStoredSession();
311311

312-
unregisterReceiver(mTermuxActivityBroadcastReceiever);
312+
unregisterTermuxActivityBroadcastReceiever();
313313
getDrawer().closeDrawers();
314314
}
315315

@@ -716,47 +716,79 @@ public TermuxAppSharedProperties getProperties() {
716716
public static void updateTermuxActivityStyling(Context context) {
717717
// Make sure that terminal styling is always applied.
718718
Intent stylingIntent = new Intent(TERMUX_ACTIVITY.ACTION_RELOAD_STYLE);
719-
stylingIntent.putExtra(TERMUX_ACTIVITY.EXTRA_RELOAD_STYLE, "styling");
720719
context.sendBroadcast(stylingIntent);
721720
}
722721

722+
private void registerTermuxActivityBroadcastReceiver() {
723+
IntentFilter intentFilter = new IntentFilter();
724+
intentFilter.addAction(TERMUX_ACTIVITY.ACTION_REQUEST_PERMISSIONS);
725+
intentFilter.addAction(TERMUX_ACTIVITY.ACTION_RELOAD_STYLE);
726+
727+
registerReceiver(mTermuxActivityBroadcastReceiver, intentFilter);
728+
}
729+
730+
private void unregisterTermuxActivityBroadcastReceiever() {
731+
unregisterReceiver(mTermuxActivityBroadcastReceiver);
732+
}
733+
734+
private void fixTermuxActivityBroadcastReceieverIntent(Intent intent) {
735+
if (intent == null) return;
736+
737+
String extraReloadStyle = intent.getStringExtra(TERMUX_ACTIVITY.EXTRA_RELOAD_STYLE);
738+
if ("storage".equals(extraReloadStyle)) {
739+
intent.removeExtra(TERMUX_ACTIVITY.EXTRA_RELOAD_STYLE);
740+
intent.setAction(TERMUX_ACTIVITY.ACTION_REQUEST_PERMISSIONS);
741+
}
742+
}
743+
723744
class TermuxActivityBroadcastReceiver extends BroadcastReceiver {
724745
@Override
725746
public void onReceive(Context context, Intent intent) {
747+
if (intent == null) return;
748+
726749
if (mIsVisible) {
727-
String whatToReload = intent.getStringExtra(TERMUX_ACTIVITY.EXTRA_RELOAD_STYLE);
728-
Logger.logDebug(LOG_TAG, "Reloading termux style for: " + whatToReload);
729-
if ("storage".equals(whatToReload)) {
730-
if (ensureStoragePermissionGranted())
731-
TermuxInstaller.setupStorageSymlinks(TermuxActivity.this);
732-
return;
750+
fixTermuxActivityBroadcastReceieverIntent(intent);
751+
752+
switch (intent.getAction()) {
753+
case TERMUX_ACTIVITY.ACTION_REQUEST_PERMISSIONS:
754+
Logger.logDebug(LOG_TAG, "Received intent to request storage permissions");
755+
if (ensureStoragePermissionGranted())
756+
TermuxInstaller.setupStorageSymlinks(TermuxActivity.this);
757+
return;
758+
case TERMUX_ACTIVITY.ACTION_RELOAD_STYLE:
759+
Logger.logDebug(LOG_TAG, "Received intent to reload styling");
760+
reloadTermuxActivityStyling();
761+
return;
762+
default:
733763
}
764+
}
765+
}
766+
}
734767

735-
if(mTermuxTerminalSessionClient != null) {
736-
mTermuxTerminalSessionClient.checkForFontAndColors();
737-
}
768+
private void reloadTermuxActivityStyling() {
769+
if(mTermuxTerminalSessionClient != null) {
770+
mTermuxTerminalSessionClient.checkForFontAndColors();
771+
}
738772

739-
if(mProperties!= null) {
740-
mProperties.loadTermuxPropertiesFromDisk();
773+
if(mProperties!= null) {
774+
mProperties.loadTermuxPropertiesFromDisk();
741775

742-
if (mExtraKeysView != null) {
743-
mExtraKeysView.reload(mProperties.getExtraKeysInfo());
744-
}
745-
}
776+
if (mExtraKeysView != null) {
777+
mExtraKeysView.reload(mProperties.getExtraKeysInfo());
778+
}
779+
}
746780

747-
setTerminalToolbarHeight();
781+
setTerminalToolbarHeight();
748782

749-
setSoftKeyboardState();
783+
setSoftKeyboardState();
750784

751-
// To change the activity and drawer theme, activity needs to be recreated.
752-
// But this will destroy the activity, and will call the onCreate() again.
753-
// We need to investigate if enabling this is wise, since all stored variables and
754-
// views will be destroyed and bindService() will be called again. Extra keys input
755-
// text will we restored since that has already been implemented. Terminal sessions
756-
// and transcripts are also already preserved. Theme does change properly too.
757-
// TermuxActivity.this.recreate();
758-
}
759-
}
785+
// To change the activity and drawer theme, activity needs to be recreated.
786+
// But this will destroy the activity, and will call the onCreate() again.
787+
// We need to investigate if enabling this is wise, since all stored variables and
788+
// views will be destroyed and bindService() will be called again. Extra keys input
789+
// text will we restored since that has already been implemented. Terminal sessions
790+
// and transcripts are also already preserved. Theme does change properly too.
791+
// TermuxActivity.this.recreate();
760792
}
761793

762794

0 commit comments

Comments
 (0)