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

[TIMOB-19878] Orientation Change is not correctly detected on Android #9525

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@
import org.appcelerator.titanium.util.TiOrientationHelper;
import org.appcelerator.titanium.util.TiSensorHelper;

import android.content.res.Configuration;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.DisplayMetrics;
import android.view.Display;

import static org.appcelerator.titanium.util.TiOrientationHelper.ORIENTATION_LANDSCAPE;
import static org.appcelerator.titanium.util.TiOrientationHelper.ORIENTATION_LANDSCAPE_REVERSE;
import static org.appcelerator.titanium.util.TiOrientationHelper.ORIENTATION_PORTRAIT;
import static org.appcelerator.titanium.util.TiOrientationHelper.ORIENTATION_PORTRAIT_REVERSE;

@Kroll.module @ContextSpecific
public class GestureModule extends KrollModule
implements SensorEventListener
public class GestureModule extends KrollModule implements SensorEventListener
{
private static final String TAG = "GestureModule";
private static final String EVENT_ORIENTATION_CHANGE = "orientationchange";
Expand Down Expand Up @@ -150,7 +152,7 @@ public void onSensorChanged(SensorEvent event)
lastEventInShake = currentEventInShake;

Log.d(TAG, "ACC-Shake : threshold: " + threshold + " force: " + force + " delta : " + force + " x: " + x
+ " y: " + y + " z: " + z, Log.DEBUG_MODE);
+ " y: " + y + " z: " + z, Log.DEBUG_MODE);
} else {
if (shakeInitialized && inShake) {
if (difftime > postShakePeriod) {
Expand All @@ -163,7 +165,7 @@ public void onSensorChanged(SensorEvent event)
data.put("y", y);
data.put("z", z);
fireEvent(EVENT_SHAKE, data);

Log.d(TAG, "Firing shake event (x:" + x + " y:" + y + " z:" + z + ")", Log.DEBUG_MODE);
}
}
Expand All @@ -174,42 +176,47 @@ public void onSensorChanged(SensorEvent event)
shakeInitialized = true;
}
}

@Kroll.getProperty @Kroll.method
public boolean isPortrait()
{
// Deprecated in 6.1.0 in parity-favor of Ti.Gesture.portrait
return TiApplication.getInstance().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
// // Deprecated in 6.1.0 in parity-favor of Ti.Gesture.portrait
Activity activity = TiApplication.getAppRootOrCurrentActivity();
int orientation = TiOrientationHelper.getWindowTiOrientationModeFrom(activity);
return (orientation == ORIENTATION_PORTRAIT) || (orientation == ORIENTATION_PORTRAIT_REVERSE);

}

@Kroll.getProperty @Kroll.method
public boolean isLandscape()
{
// Deprecated in 6.1.0 in parity-favor of Ti.Gesture.landscape
return TiApplication.getInstance().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
Activity activity = TiApplication.getAppRootOrCurrentActivity();
int orientation = TiOrientationHelper.getWindowTiOrientationModeFrom(activity);
return (orientation == ORIENTATION_LANDSCAPE) || (orientation == ORIENTATION_LANDSCAPE_REVERSE);
}

@Kroll.getProperty @Kroll.method
public boolean getPortrait()
{
return TiApplication.getInstance().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
Activity activity = TiApplication.getAppRootOrCurrentActivity();
int orientation = TiOrientationHelper.getWindowTiOrientationModeFrom(activity);
return (orientation == ORIENTATION_PORTRAIT) || (orientation == ORIENTATION_PORTRAIT_REVERSE);
}

@Kroll.getProperty @Kroll.method
public boolean getLandscape()
{
return TiApplication.getInstance().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
Activity activity = TiApplication.getAppRootOrCurrentActivity();
int orientation = TiOrientationHelper.getWindowTiOrientationModeFrom(activity);
return (orientation == ORIENTATION_LANDSCAPE) || (orientation == ORIENTATION_LANDSCAPE_REVERSE);
}

@Kroll.getProperty @Kroll.method
public int getOrientation()
{
DisplayMetrics dm = new DisplayMetrics();
Display display = TiApplication.getAppRootOrCurrentActivity().getWindowManager().getDefaultDisplay();
display.getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
return TiOrientationHelper.convertRotationToTiOrientationMode(display.getRotation(), width, height);
Activity activity = TiApplication.getAppRootOrCurrentActivity();
return TiOrientationHelper.getWindowTiOrientationModeFrom(activity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
*/
package org.appcelerator.titanium.util;

import android.content.res.Configuration;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import org.appcelerator.kroll.common.Log;

@SuppressWarnings("deprecation")
public class TiOrientationHelper
Expand All @@ -18,54 +23,73 @@ public class TiOrientationHelper
public static final int ORIENTATION_LANDSCAPE = 2;
public static final int ORIENTATION_PORTRAIT_REVERSE = 3;
public static final int ORIENTATION_LANDSCAPE_REVERSE = 4;
public static final int ORIENTATION_SQUARE = 5;

public static int convertRotationToTiOrientationMode (int rotation, int width, int height)
{
// Device that has portrait natural orientation
if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
Log.d("convertRotationToTiOrientationMode","rotation = "+ rotation + " width= " + width +" height=" + height);
// Device that has portrait natural orientation
if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
Log.d("convertRotationToTiOrientationMode","Portrait natural orientation");
switch (rotation)
{
case Surface.ROTATION_0:
return ORIENTATION_PORTRAIT;

case Surface.ROTATION_90:
return ORIENTATION_LANDSCAPE;

switch (rotation)
{
case Surface.ROTATION_0:
return ORIENTATION_PORTRAIT;
case Surface.ROTATION_180:
return ORIENTATION_PORTRAIT_REVERSE;

case Surface.ROTATION_90:
return ORIENTATION_LANDSCAPE;
case Surface.ROTATION_270:
return ORIENTATION_LANDSCAPE_REVERSE;

case Surface.ROTATION_180:
return ORIENTATION_PORTRAIT_REVERSE;
default:
return ORIENTATION_UNKNOWN;
}

case Surface.ROTATION_270:
return ORIENTATION_LANDSCAPE_REVERSE;
// Device that has landscape natural orientation. Eg Samsung Galaxy Tab 3 10.1
} else {
Log.d("convertRotationToTiOrientationMode","Landscape natural orientation");
switch (rotation)
{
case Surface.ROTATION_0:
return ORIENTATION_LANDSCAPE;

default:
return ORIENTATION_UNKNOWN;
}
case Surface.ROTATION_90:
return ORIENTATION_PORTRAIT;

// Device that has landscape natural orientation. Eg Samsung Galaxy Tab 3 10.1
} else {
switch (rotation)
{
case Surface.ROTATION_0:
return ORIENTATION_LANDSCAPE;
case Surface.ROTATION_180:
return ORIENTATION_LANDSCAPE_REVERSE;

case Surface.ROTATION_90:
return ORIENTATION_PORTRAIT;
case Surface.ROTATION_270:
return ORIENTATION_PORTRAIT_REVERSE;

case Surface.ROTATION_180:
return ORIENTATION_LANDSCAPE_REVERSE;
default:
return ORIENTATION_UNKNOWN;
}

case Surface.ROTATION_270:
return ORIENTATION_PORTRAIT_REVERSE;
}

default:
return ORIENTATION_UNKNOWN;
}
}

}

public static int getWindowTiOrientationModeFrom(Activity activity)
{
int orientation = ORIENTATION_UNKNOWN;
if (activity != null) {
WindowManager windowManager = activity.getWindowManager();
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
if (display != null) {
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
orientation = convertRotationToTiOrientationMode(
display.getRotation(), dm.widthPixels, dm.heightPixels);
}
}
}
return orientation;
}
}