简体   繁体   中英

Suppress / detect rotation in Android using Javascript

I'm trying to implement a wrapper around a modified VNC viewer applet (VNC = a remote display protocol) to provide an enhanced user experience (using javascript) but have run into problems with handling rotation.

The width/height of the applet are fixed (1) - so when the user rotates the device, the applet overlows the screen or is resized:

+------------+   +-------+         +-------+
|............|   |.......|...      |.......|
|.         X.|   |.      | X. or   |.    x.|
|.          .| > |.      |  .      |.......|
|............|   |.......|...      |       |
+------------+   |       |         |       |
                 |       |         |       |
                 +-------+  :(     +-------+ :(

But I want....

+------------+   +-------+
|............|   |.......|
|.         X.|   |.     .|
|.          .| > |.     .|
|............|   |.     .|
+------------+   |.    X.|
                 |.......|
                 +-------+  :)

While I can detect the autorotation from the change in screen dimenions in javascript, and, if it were written in Java, I could suppress the autorotation , neither of these achieves my desired result: specifically, that

  • the orientation of the display would not change when the device is rotated
  • that my javascript would detect that device has been rotated and re-orient the content of the applet (by sending an OOB message to run xrandr at the vncserver).

I'm not totally averse to implementing the detection of rotation as a seperate applet exposed to the javascript / implementing the page via a webview but would prefer no to.

Is it possible to lock the orientation in a html app manifest file?

Is it possible to detect rotation using javascript when the orientation is locked?

Is there another way to solve this?

1 - this is a limitation of the underlying protocol, I'm already making changes to the Java applet, I don't really want to have to rewrite the server too!

Update

I'm aware that most versions of VNC (inclucding noVNC) have a problem with xrandr.

If someone can tell me how to suppress the automatic re-orientation of a browser page (without using an applet with a webview) that at least givers me part of the answer.

I'm not entirely sure if I might perhaps be missing your point here, but you can prevent the device from re-laying out on rotation or keyboard slide out by adding the following line to your manifest:

android:configChanges="keyboardHidden|orientation"

Then, if I understand correctly you can have your javascript simply rotate the text within the applet once the change happens.

This will provide the desired effect of maintaining the window in the direction and size you have indicated above, leaving it up to yourself to correct the text direction.

Have a look at this, someone seems to have already solved this problem: https://stackoverflow.com/a/2307936/375929

UPD Afaik, you can't enforce orientation stuff from javascript, but you can detect rotations and react with a complementary rotation in your webapp - so orientation of your webapp remains the same to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM