termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.34k stars 459 forks source link

termux-wallpaper doesn't change wallpaper when run by sudo #633

Closed FalconSN closed 1 year ago

FalconSN commented 1 year ago

Problem description I wrote a small script to change the wallpaper at specific times, and need to run it as root to avoid it getting killed when Termux closes. However, it does not change the wallpaper when run with sudo -E -u 10176. 10176 is Termux UID. It does make exit immediately with error code 1.

Steps to reproduce sudo -E -u $(id -u) termux-wallpaper -f <file>

Expected behavior Change the wallpaper.

Additional information I don't know if this is a problem on tsu side or termux-wallpaper. I will report this in tsu repo if I'm being told so.

agnostic-apollo commented 1 year ago

Run termux-wallpaper command with RUN_COMMAND intent, otherwise socket connection to termux-api app will fail, lot of duplicate issues for that.

https://github.com/termux/termux-app/wiki/RUN_COMMAND-Intent

And just running with root will not keep process alive, check https://www.reddit.com/r/termux/comments/yuwr4f/keeping_the_script_alive_even_after_termux_app_is/

FalconSN commented 1 year ago

lot of duplicate issues for that.

Apologizes, I initially intented to open a discussion for this and not issue, but seemingly there's no way to ask questions without opening issues. Can you maybe consider this?

And just running with root will not keep process alive, check

Yes, I was turning the script into root mode after I saw this reddit post.

There's one another issue, though, and I don't want to open another issue for I'm quite inexperienced in Github. When the phone is in landscape mode in an app, termux-wallpaper is changing the image's aspect ratio to landscape too, thus causing a 1080x2340 image to be cropped to 1080x640˜ and stretched. What should be done for this?

agnostic-apollo commented 1 year ago

You can open a discussion at https://github.com/termux/termux-api/discussions

When the phone is in landscape mode in an app, termux-wallpaper is changing the image's aspect ratio to landscape too, thus causing a 1080x2340 image to be cropped to 1080x640˜ and stretched. What should be done for this?

Termux only provides image to android and there doesn't seem to be any rotation argument, so if your phone is in landscape, wallpaper is likely going to be set in that way, might be a launcher issue. Either rotate phone to portrait before setting wallpaper or try rotating image file before passing to termux.

https://github.com/termux/termux-api/blob/b7323274a64e708d573faa8bf78c1bb42c9d81ea/app/src/main/java/com/termux/api/apis/WallpaperAPI.java#L126

https://developer.android.com/reference/android/app/WallpaperManager#setBitmap(android.graphics.Bitmap,%20android.graphics.Rect,%20boolean,%20int)

FalconSN commented 1 year ago

there doesn't seem to be any rotation argument

When we dumpsys display | grep 'DisplayInfo' ; we get 3 different informations: mStaticDisplayInfo, mBaseDisplayInfo, mOverrideDisplayInfo So, to my understanding, getApplicationContext() in:

https://github.com/termux/termux-api/blob/b7323274a64e708d573faa8bf78c1bb42c9d81ea/app/src/main/java/com/termux/api/apis/WallpaperAPI.java#L120C32-L120C32

Returns the third one, which probably, again to my understanding, only refers to current layer, such as when in a landscape only app, while getBaseContext() returns the second one, which is most likely default layer. I have changed that part to getBaseContext() and it's working as I desired. There are some other examples of using it like getBaseContext().getWallpaper(), but I should leave it to someone who knows what they are doing.

I don't know how to use code snippets, sorry.

agnostic-apollo commented 1 year ago

Are you saying that if you change

Context context = getApplicationContext();
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);

to

Context context = getBaseContext();
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);

and then recompile the app and install it, it fixes the aspect ratio issue?

Provide screenshots of normal and landscape wallpapers states, the image file, and which launcher you are using. And what do you mean by landscape mode, rotation locked in landscape or auto rotate enabled and phone in landscape. Launchers also have their own orientation setting. By default nova does not rotate launcher homescreen or wallpaper, unless look and feel -> screen orientation is changed to Auto rotate.

FalconSN commented 1 year ago

Well, this got confusing. Yesterday it was setting the wallpaper in portrait when phone was brought to landscape mode by auto-rotation or apps/games, and today it doesn't. I will look for a better solution in Java, but cannot make any promises.

agnostic-apollo commented 1 year ago

The Context type wouldn't have any effect on wallpaper orientation. All it is used is to get the wallpaper service, and context type wouldn't change behaviour of wallpaper setting, that's why I asked to confirm.

https://cs.android.com/android/platform/superproject/+/android-13.0.0_r74:frameworks/base/core/java/android/app/WallpaperManager.java;l=745

That dumpsys Base reference doesn't have anything to do with base Context.

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/display/LogicalDisplay.java

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/display/LocalDisplayAdapter.java