Android Open Source - QuestCompass Screen Orienation Event Listener






From Project

Back to project page QuestCompass.

License

The source code is released under:

Apache License

If you think the Android project QuestCompass listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.renard.radar;
// w  w  w  .  j  a  v a  2s .co m
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
import android.view.Surface;

/**
 * listens to device screen orienation changes reports back the degrees a view
 * must be rotated to be horizontal.
 * I use this class to keep the labels always in a horizontal position.
 * 
 * @author renard
 * 
 */
public class ScreenOrienationEventListener extends OrientationEventListener {

  private int mCurrentOrientation = -1;

  public interface OnScreenOrientationChangeListener {
    void onScreenOrientationChanged(final int orientation);
    void onScreenRotationChanged(final int degrees);
  };

  private final OnScreenOrientationChangeListener mListener;

  public ScreenOrienationEventListener(Context context, OnScreenOrientationChangeListener listener) {
    super(context, SensorManager.SENSOR_DELAY_NORMAL);
    mListener = listener;
  }

  private int getOrientationFromDegrees(final int orientation) {
    if (orientation <= 45) {
      return Surface.ROTATION_0;
    } else if (orientation <= 135) {
      return Surface.ROTATION_90;
    } else if (orientation <= 225) {
      return Surface.ROTATION_180;
    } else if (orientation <= 315) {
      return Surface.ROTATION_270;
    }
    return Surface.ROTATION_0;
  }

  @Override
  public void onOrientationChanged(int orientation) {
    if (orientation == ORIENTATION_UNKNOWN) {
      return;
    }
    final int newOrientation = getOrientationFromDegrees(orientation);
    mListener.onScreenRotationChanged(orientation);

    if (mCurrentOrientation == newOrientation) {
      return;
    }
    mListener.onScreenOrientationChanged(newOrientation);
    mCurrentOrientation = newOrientation;
  }
}




Java Source Code List

de.renard.radar.CompassFilter.java
de.renard.radar.CompassSensorListener.java
de.renard.radar.LocationAdapter.java
de.renard.radar.LocationDescriptionDialog.java
de.renard.radar.LocationListDaoGenerator.java
de.renard.radar.LocationListDialog.java
de.renard.radar.RadarActivity.java
de.renard.radar.ScreenOrienationEventListener.java
de.renard.radar.SensorDataController.java
de.renard.radar.Util.java
de.renard.radar.map.LocationOverlay.java
de.renard.radar.map.LocationPickActivity.java
de.renard.radar.views.LocationDeleteInterface.java
de.renard.radar.views.RadarView.java
de.renard.radar.views.RotateView.java