termux / termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.
https://f-droid.org/en/packages/com.termux
Other
31.28k stars 3.32k forks source link

[Feature]: overlay for preventing termux-app from going to background #3903

Open twaik opened 1 month ago

twaik commented 1 month ago

Feature description

When user switches to termux-x11's Activity termux-app goes foreground and its capability to draw frames is limited which causes freezes. There are a few possible solutions to fix this.

  1. Disable battery restrictions for termux-app.
  2. Enable wakelock.
  3. Using termux-float (need to start termux-x11 session from floating session, not in session of termux-app which is in background).
  4. Creating overlay from termux-app's service.

Creating overlay while enabling wakelock should fix all issue. Also I think it will be good to ask to disable battery restrictions when enabling wakelock.

Additional information

A part of conversation in private chat in tg. twaik: > I am not an expert in this, but maybe you can try to build termux version which creates overlay when service starts? Only as a proof of concept. wasp: > i can try. in the worse situation, we can just tell users to disable termux's battery optimization if there's freeze twaik: > But that will not work in the case of chroot. But maybe that was a different freeze wasp: > any related issues? i've only found this, about huawei device. https://github.com/termux/termux-x11/issues/516 twaik: > It was reported to telegram of another developer. Now I can not even find this. twaik: > When you tried termux:float, did you invoke termux-x11 and session from there or from regular termux? wasp: > from regular termux wasp: > after creating an overlay view from termux, termux-x11 seems not to freeze anymore, i'll have more test wasp: > I just realized that termux:float start a new session rather than using the current session of termux. now by starting termux-x11 from termux:float, it doesn't freeze either! twaik: > Are you sure? There is no conditions like plugged in usb or something? wasp: > Here's the video. First launch with termux float. Then killed float. Launch with normal termux https://github.com/termux/termux-app/assets/9674930/11353370-316b-477d-b7f5-d780ee8c1e70 wasp: > Here's the result of overlay view from termux. Also not freeze https://github.com/termux/termux-app/assets/9674930/789cabb4-78b5-49a0-b2e9-22a1855e59ac twaik: > What is a minimal size of an overlay? Can it be 1x1 pixel big? Can it be completely transparent? Can it be put to left top corner of screen under statusbar? wasp: > 4x4 pixels, alpha=0.01 (range from 0~1) seems fine for me wasp: > this is the code i use to create overlay in termux app. the method is called in `TermuxActivity.onCreate` ```java private void createOverlayView() { if(!android.provider.Settings.canDrawOverlays(this)){ startActivity(new Intent( "android.settings.action.MANAGE_OVERLAY_PERMISSION", android.net.Uri.parse("package:" + getPackageName()))); return; } WindowManager windowManager = getSystemService(WindowManager.class); //操作面板 android.widget.TextView textView = new android.widget.TextView(this); textView.setText("测试Overlay"); textView.setBackgroundColor( android.graphics.Color.RED); android.view.WindowManager.LayoutParams rootParams = new android.view.WindowManager.LayoutParams(4, 4, 2038, 32 | 8, 1);//TYPE_APPLICATION_OVERLAY, FLAG_NOT_TOUCH_MODAL | FLAG_NOT_FOCUSABLE, RGBA_8888); rootParams.gravity = android.view.Gravity.START | android.view.Gravity.TOP; rootParams.alpha = 0.05f; windowManager.addView(textView, rootParams); } ```
twaik commented 1 month ago

And yes, I tried to create overlay from termux-x11 process, but it seems to be very complicated because of vendor modifications and checks if process if valid.

ewt45 commented 1 month ago

i'm the wasp in the conversation.

after more test, i found that wakelock is not helping. So it should be the battery optimization option. here's an example of requesting it

if (!getSystemService(PowerManager.class).isIgnoringBatteryOptimizations(getPackageName()))
    startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);

also i'm not sure if it's a violance to create a nearly invisible overlay view. if so maybe just a command like termux-disable-battery-opt is enough?

agnostic-apollo commented 1 month ago

Termux app already requests to disable battery optimizations when enabling wakelock. Wakelock itself should not help with cpu issues while screen is still on, it's meant to disable cpu and network throttling while screen is off, which slows/halts commands from running if phone goes to sleep/doze mode and commands work again at normal speed when screen is awoken again.

As for frames dropping.

ewt45 commented 1 month ago

Termux app already requests to disable battery optimizations when enabling wakelock.

I've tried clicking the wakelock button. it once requests to disable battery optimiazations normally, but now there's no request. maybe I messed up with something. I'll check later.

frame dropping: it's actually total freeze. no frame will be updated, but xserver itself is not killed. overlay or disabling battery optimizations, one of them will solve the problem. overlay not only applies to an overlay view, but also termux-app itself running as floating window mode.

agnostic-apollo commented 1 month ago

Wakelock button will not ask again if battery optimizations have already been disabled earlier.

it's actually total freeze. no frame will be updated, but xserver itself is not killed.

Its not gonna get killed, it just wouldn't get cpu time to run untill app is in foreground again.

overlay or disabling battery optimizations, one of them will solve the problem.

Well, find out, no need to spend time adding overlay in the app if disabling battery optimizations already fixes the issue.

ewt45 commented 1 month ago

sorry, my memory doesn't serve. it's another setting that works beside overlay, rather than battery optimiazation. In phone's settings -> battery -> battery usage details -> Launch settings, enable the Run in background option.

This is more likely a rom-dependent option, i think most of the roms have the similar option but there's no universal entry.

as for disabling battery optimisation, it seems that on my tablet(huawei matepad 11, android12) it takes no effect. here are some tests(all without overlay):

twaik commented 1 month ago

I did not find such an option on my Samsung devices and Pritom P7 tablet. Probably it can be missing on other devices. I think it will be a good idea to create overlay in the case if termux-app already has Draw over apps permission, but not request for it explicitly when user acquires wakelock.