Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle and launch on multiple screens #976

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions termux-app/terminal-term/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<application
android:extractNativeLibs="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ClipData;
Expand All @@ -16,14 +17,18 @@
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.display.DisplayManager;
import android.hardware.usb.UsbManager;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import androidx.annotation.NonNull;
Expand All @@ -39,12 +44,14 @@
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
Expand Down Expand Up @@ -199,6 +206,56 @@ public boolean ensureStoragePermissionGranted() {
}
}

public void makeFullscreen() {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
Window window = getWindow();
View view = window.getDecorView();

// rerun the serviceIntent, since we might need to switch screens if they attached/detached
if ((mListViewAdapter != null) && (mListViewAdapter.getCount() != 0)) {
Intent serviceIntent = new Intent(this, TermuxService.class);
serviceIntent.putExtra("username", username);
serviceIntent.putExtra("hostname", hostname);
serviceIntent.putExtra("port", port);
serviceIntent.putExtra("sessionName", sessionName);
serviceIntent.setAction(TermuxService.ACTION_EXECUTE);
startService(serviceIntent);
}

// hide/show soft input depending on the availability of hardware input devices
boolean was_fullscreen = false;
if (getResources().getConfiguration().keyboard == Configuration.KEYBOARD_NOKEYS) {
mgr.showSoftInput(mTerminalView, InputMethodManager.SHOW_IMPLICIT);
} else {
mgr.showSoftInput(mTerminalView, InputMethodManager.HIDE_IMPLICIT_ONLY);
// if we are on a separate display, keep that display on and in fullscreen while we are on it
if (window.getWindowManager().getDefaultDisplay().getDisplayId() != Display.DEFAULT_DISPLAY) {
// FLAG_KEEP_SCREEN_ON is separate from wakelock, specific to the screen this activity is on;
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
was_fullscreen = true;
}
}

if (!was_fullscreen) {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}

// when gaining focus, we may or may not get SystemUiVisibilityChange event
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) makeFullscreen();
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Expand All @@ -210,14 +267,41 @@ public void onCreate(Bundle bundle) {
setContentView(R.layout.drawer_layout);
mTerminalView = findViewById(R.id.terminal_view);
mTerminalView.setOnKeyListener(new TermuxViewClient(this));

mTerminalView.setTextSize(mSettings.getFontSize());

mTerminalView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override public void onSystemUiVisibilityChange(int visibility) { makeFullscreen(); }
});

final BroadcastReceiver changeReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
makeFullscreen();
}
};

// bluetooth/usb may be used to attach/detach input devices; listen to them.
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(changeReceiver, filter);

// displays may be added/removed, and we may need to move activities to/from them
((DisplayManager)getSystemService(DISPLAY_SERVICE)).registerDisplayListener(new DisplayManager.DisplayListener() {
@Override public void onDisplayAdded(int i) { makeFullscreen(); }
@Override public void onDisplayRemoved(int i) { makeFullscreen(); }
@Override public void onDisplayChanged(int i) { makeFullscreen(); }
}, null);

mTerminalView.requestFocus();

final ViewPager viewPager = findViewById(R.id.viewpager);
if (mSettings.isShowExtraKeys()) viewPager.setVisibility(View.VISIBLE);


ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
layoutParams.height = layoutParams.height * mSettings.mExtraKeys.length;
viewPager.setLayoutParams(layoutParams);
Expand Down Expand Up @@ -517,7 +601,17 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {

if (mTermService == null) return; // Activity might have been destroyed.
try {
addNewSession(false, null);
if (mListViewAdapter.getCount() == 0) {
Log.i( EmulatorDebug.LOG_TAG,"Adding new session: onServiceConnected: "+componentName.flattenToShortString()+", "+service.toString());

addNewSession(false, null);
} else {
// activate the first session
TerminalSession activatedSession = mListViewAdapter.getItem(0);
switchToSession(activatedSession);
getDrawer().closeDrawers();

}
} catch (WindowManager.BadTokenException e) {
// Activity finished - ignore.
}
Expand Down Expand Up @@ -786,6 +880,7 @@ public boolean onContextItemSelected(MenuItem item) {
session.reset();
showToast(getResources().getString(R.string.reset_toast_notification), true);
}
makeFullscreen();
return true;
}
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.termux.app;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand All @@ -9,6 +11,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
Expand All @@ -17,6 +21,7 @@
import android.os.PowerManager;
import android.system.Os;
import android.util.Log;
import android.view.Display;
import android.widget.ArrayAdapter;

import com.termux.R;
Expand Down Expand Up @@ -137,7 +142,22 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(EmulatorDebug.LOG_TAG, "Currently only intents from UserLAnd are supported");
} else {
// Launch the main Termux app, which will now show the current session:
startActivity(new Intent(this, TermuxActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Intent si = new Intent(this, TermuxActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
ActivityOptions options = ActivityOptions.makeBasic();
DisplayManager dm = (DisplayManager)getSystemService(DISPLAY_SERVICE);
Display ds[] = dm.getDisplays();
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for(Display d: ds) {
if ((d.getState() > Display.STATE_OFF) && ((d.getFlags() & Display.FLAG_PRESENTATION) != 0) && ((d.getFlags() & Display.FLAG_PRIVATE) == 0)) {
options.setLaunchBounds(null);
options.setLaunchDisplayId(d.getDisplayId());
break;
}
}
startActivity(si, options.toBundle());
} else
startActivity(si);
}
} else if (action != null) {
Log.e(EmulatorDebug.LOG_TAG, "Unknown TermuxService action: '" + action + "'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.termux.app;

import android.content.Context;
import android.content.res.Configuration;
import android.media.AudioManager;
import androidx.drawerlayout.widget.DrawerLayout;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.termux.terminal.KeyHandler;
Expand Down Expand Up @@ -39,8 +41,7 @@ public float onScale(float scale) {

@Override
public void onSingleTapUp(MotionEvent e) {
InputMethodManager mgr = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mActivity.mTerminalView, InputMethodManager.SHOW_IMPLICIT);
mActivity.makeFullscreen();
}

@Override
Expand Down