I'd like to display my website only in landscape mode when viewed from modern smartphones. Is there a way to force the major mobile browsers- Chrome/Firefox/Safari/IE10 to do so? Basically I'd like them to start the browser in landscape mode, irrespective of screen orientation and rotation lock.
Is it too much to ask? I think not because certainly there are games that only play in landscape mode, and the Windows Phone UI home screen uses only portrait mode.
Well, you can't force browsers orientation to landscape, but you sure can try to detect device orientation and suggest the user to rotate his/her device (through a full screen <div> and hide it when the user rotated the display)!
if (window.DeviceOrientationEvent) {
console.log("DeviceOrientation is supported");
}
Then add the appropriate listeners once you know device orientation is supported:
if (window.DeviceOrientationEvent) {
// Listen for the event and handle DeviceOrientationEvent object
window.addEventListener('deviceorientation', devOrientHandler, false);
}
Then, handle the event:
if (window.DeviceOrientationEvent) {
document.getElementById("doEvent").innerHTML = "DeviceOrientation";
// Listen for the deviceorientation event and handle the raw data
window.addEventListener('deviceorientation', function(eventData) {
// gamma is the left-to-right tilt in degrees, where right is positive
var tiltLR = eventData.gamma;
// beta is the front-to-back tilt in degrees, where front is positive
var tiltFB = eventData.beta;
// alpha is the compass direction the device is facing in degrees
var dir = eventData.alpha
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir);
}, false);
} else {
document.getElementById("doEvent").innerHTML = "Not supported."
}
...and display the result (i.e hide the full screen suggestion <div>)!
document.getElementById("doTiltLR").innerHTML = Math.round(tiltLR);
document.getElementById("doTiltFB").innerHTML = Math.round(tiltFB);
document.getElementById("doDirection").innerHTML = Math.round(dir);
// Apply the transform to the image
var logo = document.getElementById("imgLogo");
logo.style.webkitTransform =
"rotate("+ tiltLR +"deg) rotate3d(1,0,0, "+ (tiltFB*-1)+"deg)";
logo.style.MozTransform = "rotate("+ tiltLR +"deg)";
logo.style.transform =
"rotate("+ tiltLR +"deg) rotate3d(1,0,0, "+ (tiltFB*-1)+"deg)";
Reference: http://www.html5rocks.com/en/tutorials/device/orientation/
Hope that helped :)
Related
I am developing an Android application that uses accessibility-service, and I want to disable explore-by-touch when the user opens specific applications like Piano, Games, etc.
I read API documentation of setTouchExplorationPassthroughRegion()
public void setTouchExplorationPassthroughRegion (int displayId,
Region region)
When AccessibilityServiceInfo#FLAG_REQUEST_TOUCH_EXPLORATION_MODE is
enabled, this function requests that touch interactions starting in
the specified region of the screen bypass the touch explorer and go
straight to the view hierarchy. There can only be one-touch
exploration passthrough region per display. Requesting a new touch
explorationpassthrough region clears the existing one. To disable this
passthrough and return to the original behaviour, pass in an empty
region. When
AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE is
disabled this function has no effect.
So I tried the above function in Android-11 but explore-by-touch is not getting disabled.
What can I do to disable explore-by-touch in some applications.
Note: The user might be running his own Accessibility-Service like TalkBack screen reader which also uses the Explore-By-Touch feature.
This function is working in Android 11 and above with proper context and region
//Disabling explore by touch
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
setTouchExplorationPassthroughRegion(
Display.DEFAULT_DISPLAY, getRegionOfFullScreen(name_of_the_view.getContext()));
}
private Region getRegionOfFullScreen(Context context) {
final DisplayMetrics metrics = new DisplayMetrics();
final Display display;
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
display = dm.getDisplay(Display.DEFAULT_DISPLAY);
if (display == null) {
return new Region();
} else {
display.getRealMetrics(metrics);
return new Region(0, 0, metrics.widthPixels, metrics.heightPixels);
}
}
Don't forget to enable Explore-By-Touch if your view is not active or removed using following code
//Enabling explore by touch
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
setTouchExplorationPassthroughRegion(Display.DEFAULT_DISPLAY, new Region());
}
What I Want:
Disable user to use split screen mode for any application in his phone.
What I've already done:
To disable split screen mode, I need to detect which method is called and in that method I can further add a functionality to draw a custom view over it or quickly pull down split screen window.
I'm looking into AccessibilityEvents as well, might be I need to parse and filter some keywords to get to split screen detection.
So what can be that method in which Android will tell that user has just started to use split screen mode. And how can I then quickly pull down split screen window?
You can detect when any application goes to split screen mode if you have asked AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED event when registering for accessibility service.
Possible way to detect Split screen mode:
In the onAccessibilityEvent(AccessibilityEvent event) function we need to write event.getSource().getContentDescription(); and search for "Split" or "Dismiss" or other keywords in the string, depends upon various custom roms. Whenever application comes in split screen mode, its content description is set as 'Split Whatsapp' etc. That's how we can detect when any particular application comes in split screen mode.
Possible way to block usage of split screen mode for any app:
After detecting you need to add this line in order to make it impossible for the user to utilize split screen mode. It will just dock the current application window.
performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN)
There are other global events as well to perform an action like:
GLOBAL_ACTION_BACK
GLOBAL_ACTION_HOME
GLOBAL_ACTION_LOCK_SCREEN
GLOBAL_ACTION_NOTIFICATIONS
GLOBAL_ACTION_POWER_DIALOG
GLOBAL_ACTION_QUICK_SETTINGS
GLOBAL_ACTION_RECENTS
GLOBAL_ACTION_TAKE_SCREENSHOT
GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN
But most suitable for this scenario is: GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN
public class AppAccessibility extends AccessibilityService {
#Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo config = new AccessibilityServiceInfo();
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
if (Build.VERSION.SDK_INT >= 16) {
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
}
setServiceInfo(config);
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event != null && event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
if (event.getSource() != null && event.getSource().getContentDescription() != null) {
if (event.getSource().getContentDescription().toString().contains("Split")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN));
}
}
}
}
}
I am trying to use a LookToWalk script in my Unity VR app that should run on my Daydream View. In the "Game" Mode to preview the changes everything works as expected (I configured the script to run forward once the user camera faces 30.0 degrees downwards or more.
However when I try to build the daydream app and install it on my Google Pixel the CharacterController.SimpleMove doesn't seem to work any more.
The logs were showing that the 30.0 degree stuff was triggered as expected but no movement was seen on the daydream.
Do you know why this could be happening? Seems really strange that it runs on the "emulator" but not the 'real' device.
using UnityEngine;
using System.Collections;
public class GVRLookWalk : MonoBehaviour {
public Transform vrCamera;
public float toggleAngle = 30.0f;
public float speed = 3.0f;
private bool shouldWalk;
private CharacterController cc;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f){
shouldWalk = true;
} else {
shouldWalk = false;
}
if (shouldWalk) {
Vector3 forward = vrCamera.TransformDirection (Vector3.forward);
cc.SimpleMove (forward * speed);
}
}
Is the Camera a child of another transform? You cannot move the camera directly. "you cannot move the camera directly in Unity. Instead, the camera must be a child of another GameObject, and changes to the position and rotation must be applied to the parent’s Transform."
https://unity3d.com/learn/tutorials/topics/virtual-reality/movement-vr
I have some kind of app, where interface depends on orientation change. So I have two functions for interface portraitSet() and landscapeSet(). Here is the code how I catching up orientation change event:
$(window).on("orientationchange",function(event){
alert(event.orientation);
if (event.orientation == 'portrait')
{
$(portraitSet());
}
else if (event.orientation == 'landscape')
{
$(landscapeSet());
}
});
On launch app in portrait mode it works perfectly, but when start app in landscape mode and trying to set mobile into portrait mode my alert(event.orientation) giving me message "landscape", but mobile on portrait mode. Where is my mistake?
hi use this code.....
$(window).resize( function(){
var height = $(window).height();
var width = $(window).width();
if(width>height) {
// Landscape
alert("LANDSCAPE");
} else {
// Portrait
alert("PORTRAIT");
}
});
if you need more refer
I am trying to write an app that requires the LED flash to go into torch mode. The problem is, Android 2.1 does not support this mode and therefore I cannot support the platform yet. Wouldn't be an issue, but I am writing it for my fiance and her Epic 4G only has 2.1 right now. I found some code samples that use some undocumented API calls and therefore work on the Motorola Droid and such, but they do not work on the Epic. Does anyone have some suggestions on where to look to find code that should help me get this working?
I'm finding that torch mode is generally working fine on 2.1 but I had the same problem with the Samsung Epic and found a hack around it.
Looking at the params returned by Camera.getParameters() when run on the Samsung Epic, I noticed that the flash-modes it claims to support are: flash-mode-values=off,on,auto;
torch-mode is not listed, implying it's not supported.
However, I found that this model would still accept that mode and WOULD turn the LED on! The bad news was that when later setting the flash-mode back to auto or off left the LED still lit! It will not turn off until you call Camera.release().
I guess that's why Samsung dont include it in the list of supported!?!
So...the method I use to toggle torch in a CameraHelper class is...
/***
* Attempts to set camera flash torch/flashlight mode on/off
* #param isOn true = on, false = off
* #return boolean whether or not we were able to set it
*/
public boolean setFlashlight(boolean isOn)
{
if (mCamera == null)
{
return false;
}
Camera.Parameters params = mCamera.getParameters();
String value;
if (isOn) // we are being ask to turn it on
{
value = Camera.Parameters.FLASH_MODE_TORCH;
}
else // we are being asked to turn it off
{
value = Camera.Parameters.FLASH_MODE_AUTO;
}
try{
params.setFlashMode(value);
mCamera.setParameters(params);
String nowMode = mCamera.getParameters().getFlashMode();
if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
{
return true;
}
if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
{
return true;
}
return false;
}
catch (Exception ex)
{
MyLog.e(mLOG_TAG, this.getClass().getSimpleName() + " error setting flash mode to: "+ value + " " + ex.toString());
}
}
The activities that use this call it as follows...
private void toggleFlashLight()
{
mIsFlashlightOn = ! mIsFlashlightOn;
/**
* hack to fix an issue where the Samsung Galaxy will turn torch on,
* even though it says it doesnt support torch mode,
* but then will NOT turn it off via this param.
*/
if (! mIsFlashlightOn && Build.MANUFACTURER.equalsIgnoreCase("Samsung"))
{
this.releaseCameraResources();
this.initCamera();
}
else
{
boolean result = mCamHelper.setFlashlight(mIsFlashlightOn);
if (! result)
{
alertFlashlightNotSupported();
}
}
}
The magic that makes this work in releaseCameraResources() is that it calls Camera.release()....and then I have to reinitialize all my camera stuff for Samsung devices.
Not pretty but seems to be working for plenty of users.
Note that I do have a report of torch mode not working at all with this code on Nexus one but have been able to dig into it. It definitely works on HTC EVO and Samsung Epic.
Hope this helps.
In my case for Samsung devices I needed to set focus mode to infinity and it started to work
params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(params);
mCamera.startPreview();