55import android .annotation .TargetApi ;
66import android .app .Activity ;
77import android .app .AlertDialog ;
8+ import android .bluetooth .BluetoothDevice ;
89import android .content .ActivityNotFoundException ;
910import android .content .BroadcastReceiver ;
1011import android .content .ClipData ;
1617import android .content .IntentFilter ;
1718import android .content .ServiceConnection ;
1819import android .content .pm .PackageManager ;
20+ import android .content .res .Configuration ;
1921import android .graphics .Color ;
2022import android .graphics .Paint ;
2123import android .graphics .Typeface ;
24+ import android .hardware .display .DisplayManager ;
25+ import android .hardware .usb .UsbManager ;
2226import android .media .AudioAttributes ;
2327import android .media .SoundPool ;
2428import android .net .Uri ;
2529import android .os .Build ;
2630import android .os .Bundle ;
31+ import android .os .Handler ;
2732import android .os .IBinder ;
2833import android .os .Vibrator ;
2934import androidx .annotation .NonNull ;
3944import android .util .Log ;
4045import android .view .ContextMenu ;
4146import android .view .ContextMenu .ContextMenuInfo ;
47+ import android .view .Display ;
4248import android .view .Gravity ;
4349import android .view .LayoutInflater ;
4450import android .view .Menu ;
4551import android .view .MenuItem ;
4652import android .view .View ;
4753import android .view .ViewGroup ;
54+ import android .view .Window ;
4855import android .view .WindowManager ;
4956import android .view .inputmethod .InputMethodManager ;
5057import android .widget .ArrayAdapter ;
@@ -199,6 +206,56 @@ public boolean ensureStoragePermissionGranted() {
199206 }
200207 }
201208
209+ public void makeFullscreen () {
210+ InputMethodManager mgr = (InputMethodManager ) getSystemService (Context .INPUT_METHOD_SERVICE );
211+ Window window = getWindow ();
212+ View view = window .getDecorView ();
213+
214+ // rerun the serviceIntent, since we might need to switch screens if they attached/detached
215+ if ((mListViewAdapter != null ) && (mListViewAdapter .getCount () != 0 )) {
216+ Intent serviceIntent = new Intent (this , TermuxService .class );
217+ serviceIntent .putExtra ("username" , username );
218+ serviceIntent .putExtra ("hostname" , hostname );
219+ serviceIntent .putExtra ("port" , port );
220+ serviceIntent .putExtra ("sessionName" , sessionName );
221+ serviceIntent .setAction (TermuxService .ACTION_EXECUTE );
222+ startService (serviceIntent );
223+ }
224+
225+ // hide/show soft input depending on the availability of hardware input devices
226+ boolean was_fullscreen = false ;
227+ if (getResources ().getConfiguration ().keyboard == Configuration .KEYBOARD_NOKEYS ) {
228+ mgr .showSoftInput (mTerminalView , InputMethodManager .SHOW_IMPLICIT );
229+ } else {
230+ mgr .showSoftInput (mTerminalView , InputMethodManager .HIDE_IMPLICIT_ONLY );
231+ // if we are on a separate display, keep that display on and in fullscreen while we are on it
232+ if (window .getWindowManager ().getDefaultDisplay ().getDisplayId () != Display .DEFAULT_DISPLAY ) {
233+ // FLAG_KEEP_SCREEN_ON is separate from wakelock, specific to the screen this activity is on;
234+ window .addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
235+ view .setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_STABLE
236+ | View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
237+ | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
238+ | View .SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
239+ | View .SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
240+ | View .SYSTEM_UI_FLAG_IMMERSIVE );
241+ was_fullscreen = true ;
242+ }
243+ }
244+
245+ if (!was_fullscreen ) {
246+ window .clearFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
247+ view .setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_STABLE
248+ | View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
249+ | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN );
250+ }
251+ }
252+
253+ // when gaining focus, we may or may not get SystemUiVisibilityChange event
254+ @ Override
255+ public void onWindowFocusChanged (boolean hasFocus ) {
256+ super .onWindowFocusChanged (hasFocus );
257+ if (hasFocus ) makeFullscreen ();
258+ }
202259 @ Override
203260 public void onCreate (Bundle bundle ) {
204261 super .onCreate (bundle );
@@ -210,14 +267,41 @@ public void onCreate(Bundle bundle) {
210267 setContentView (R .layout .drawer_layout );
211268 mTerminalView = findViewById (R .id .terminal_view );
212269 mTerminalView .setOnKeyListener (new TermuxViewClient (this ));
213-
214270 mTerminalView .setTextSize (mSettings .getFontSize ());
271+
272+ mTerminalView .setOnSystemUiVisibilityChangeListener (new View .OnSystemUiVisibilityChangeListener () {
273+ @ Override public void onSystemUiVisibilityChange (int visibility ) { makeFullscreen (); }
274+ });
275+
276+ final BroadcastReceiver changeReceiver = new BroadcastReceiver () {
277+ public void onReceive (Context context , Intent intent ) {
278+ makeFullscreen ();
279+ }
280+ };
281+
282+ // bluetooth/usb may be used to attach/detach input devices; listen to them.
283+ IntentFilter filter = new IntentFilter ();
284+ filter .addAction (UsbManager .ACTION_USB_ACCESSORY_ATTACHED );
285+ filter .addAction (UsbManager .ACTION_USB_ACCESSORY_DETACHED );
286+ filter .addAction (UsbManager .ACTION_USB_DEVICE_ATTACHED );
287+ filter .addAction (UsbManager .ACTION_USB_DEVICE_DETACHED );
288+ filter .addAction (BluetoothDevice .ACTION_ACL_CONNECTED );
289+ filter .addAction (BluetoothDevice .ACTION_ACL_DISCONNECT_REQUESTED );
290+ filter .addAction (BluetoothDevice .ACTION_ACL_DISCONNECTED );
291+ registerReceiver (changeReceiver , filter );
292+
293+ // displays may be added/removed, and we may need to move activities to/from them
294+ ((DisplayManager )getSystemService (DISPLAY_SERVICE )).registerDisplayListener (new DisplayManager .DisplayListener () {
295+ @ Override public void onDisplayAdded (int i ) { makeFullscreen (); }
296+ @ Override public void onDisplayRemoved (int i ) { makeFullscreen (); }
297+ @ Override public void onDisplayChanged (int i ) { makeFullscreen (); }
298+ }, null );
299+
215300 mTerminalView .requestFocus ();
216301
217302 final ViewPager viewPager = findViewById (R .id .viewpager );
218303 if (mSettings .isShowExtraKeys ()) viewPager .setVisibility (View .VISIBLE );
219304
220-
221305 ViewGroup .LayoutParams layoutParams = viewPager .getLayoutParams ();
222306 layoutParams .height = layoutParams .height * mSettings .mExtraKeys .length ;
223307 viewPager .setLayoutParams (layoutParams );
@@ -796,6 +880,7 @@ public boolean onContextItemSelected(MenuItem item) {
796880 session .reset ();
797881 showToast (getResources ().getString (R .string .reset_toast_notification ), true );
798882 }
883+ makeFullscreen ();
799884 return true ;
800885 }
801886 default :
0 commit comments