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

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

Open
twaik opened this issue Mar 31, 2024 · 7 comments
Open

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

twaik opened this issue Mar 31, 2024 · 7 comments

Comments

@twaik
Copy link
Member

twaik commented Mar 31, 2024

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. termux/termux-x11#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

video_2024-03-31_10-13-58.mp4

wasp:

Here's the result of overlay view from termux. Also not freeze

video_2024-03-31_10-14-55.mp4

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

    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
Copy link
Member Author

twaik commented Mar 31, 2024

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.

@twaik twaik changed the title [Feature]: overlay for preventing termux-app to go background [Feature]: overlay for preventing termux-app from going to background Mar 31, 2024
@ewt45
Copy link

ewt45 commented Apr 1, 2024

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
Copy link
Member

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.

  • Does it happen if battery optimizations are disabled without an overlay?
  • Does it happen with overlay alone without battery optimizations being disabled? After enabling optimizations again, you will need to force stop termux app or reboot phone for changes to take effect IIRC.
  • Or are both required?

@ewt45
Copy link

ewt45 commented Apr 7, 2024

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
Copy link
Member

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
Copy link

ewt45 commented Apr 7, 2024

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):

  • reboot phone, click wakelock button, see the disabling battery optimization dialog and disable it. freeze happened.
  • reboot phone again, click wakelock button again, the battery opt is still disabled and no dialog appears. I'm not sure if this is normal, cuz on my phone (xiaomi cc9e, android9) every reboot will enable the battery opt again.
  • with run in background option enabled, disabling battery opt or not, tx11 screen won't freeze.
  • with run in background option disabled, disabling battery opt or not, tx11 screen freezes.

@twaik
Copy link
Member Author

twaik commented Apr 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants