Commit d07b0106 authored by Mounir Lamouri's avatar Mounir Lamouri
Browse files

Bug 720795 - Screen Orientation API reading and event implementation in Android. r=dougt

parent 6ac09e7c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@
namespace mozilla {
namespace dom {

// Make sure that any change here is also made in
// * mobile/android/base/GeckoScreenOrientationListener.java
// * embedding/android/GeckoScreenOrientationListener.java
enum ScreenOrientation {
  eScreenOrientation_Current            = 0,
  eScreenOrientation_PortraitPrimary    = 1,  // 00000001
+3 −0
Original line number Diff line number Diff line
@@ -512,6 +512,7 @@ abstract public class GeckoApp

        unregisterReceiver(mConnectivityReceiver);
        GeckoNetworkManager.getInstance().stop();
        GeckoScreenOrientationListener.getInstance().stop();
    }

    @Override
@@ -531,6 +532,7 @@ abstract public class GeckoApp

        registerReceiver(mConnectivityReceiver, mConnectivityFilter);
        GeckoNetworkManager.getInstance().start();
        GeckoScreenOrientationListener.getInstance().start();
    }

    @Override
@@ -586,6 +588,7 @@ abstract public class GeckoApp
        }

        GeckoNetworkManager.getInstance().stop();
        GeckoScreenOrientationListener.getInstance().stop();

        super.onDestroy();

+12 −0
Original line number Diff line number Diff line
@@ -1843,4 +1843,16 @@ public class GeckoAppShell

    // This is only used in Native Fennec.
    public static void setPreventPanning(final boolean aPreventPanning) { }

    public static short getScreenOrientation() {
        return GeckoScreenOrientationListener.getInstance().getScreenOrientation();
    }

    public static void enableScreenOrientationNotifications() {
        GeckoScreenOrientationListener.getInstance().enableNotifications();
    }

    public static void disableScreenOrientationNotifications() {
        GeckoScreenOrientationListener.getInstance().disableNotifications();
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ public class GeckoEvent {
    public static final int VISITED = 21;
    public static final int NETWORK_CHANGED = 22;
    public static final int PROXIMITY_EVENT = 23;
    public static final int SCREENORIENTATION_CHANGED = 26;

    public static final int IME_COMPOSITION_END = 0;
    public static final int IME_COMPOSITION_BEGIN = 1;
@@ -126,6 +127,8 @@ public class GeckoEvent {
    public double mBandwidth;
    public boolean mCanBeMetered;

    public short mScreenOrientation;

    public int mNativeWindow;

    public GeckoEvent() {
@@ -333,4 +336,9 @@ public class GeckoEvent {
        mBandwidth = bandwidth;
        mCanBeMetered = canBeMetered;
    }

    public GeckoEvent(short aScreenOrientation) {
        mType = SCREENORIENTATION_CHANGED;
        mScreenOrientation = aScreenOrientation;
    }
}
+122 −0
Original line number Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko;

import android.content.Context;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.Surface;

public class GeckoScreenOrientationListener
{
  static class OrientationEventListenerImpl extends OrientationEventListener {
    public OrientationEventListenerImpl(Context c) {
      super(c);
    }

    @Override
    public void onOrientationChanged(int aOrientation) {
      GeckoScreenOrientationListener.getInstance().updateScreenOrientation();
    }
  }

  static private GeckoScreenOrientationListener sInstance = null;

  // Make sure that any change in dom/base/ScreenOrientation.h happens here too.
  static public final short eScreenOrientation_PortraitPrimary    = 1;
  static public final short eScreenOrientation_PortraitSecondary  = 2;
  static public final short eScreenOrientation_LandscapePrimary   = 4;
  static public final short eScreenOrientation_LandscapeSecondary = 8;

  private short mOrientation;
  private OrientationEventListenerImpl mListener = null;

  // Whether the listener should be listening to changes.
  private boolean mShouldBeListening = false;
  // Whether the listener should notify Gecko that a change happened.
  private boolean mShouldNotify      = false;

  private GeckoScreenOrientationListener() {
    mListener = new OrientationEventListenerImpl(GeckoApp.mAppContext);
  }

  public static GeckoScreenOrientationListener getInstance() {
    if (sInstance == null) {
      sInstance = new GeckoScreenOrientationListener();
    }

    return sInstance;
  }

  public void start() {
    mShouldBeListening = true;
    updateScreenOrientation();

    if (mShouldNotify) {
      startListening();
    }
  }

  public void stop() {
    mShouldBeListening = false;

    if (mShouldNotify) {
      stopListening();
    }
  }

  public void enableNotifications() {
    updateScreenOrientation();
    mShouldNotify = true;

    if (mShouldBeListening) {
      startListening();
    }
  }

  public void disableNotifications() {
    mShouldNotify = false;

    if (mShouldBeListening) {
      stopListening();
    }
  }

  private void startListening() {
    mListener.enable();
  }

  private void stopListening() {
    mListener.disable();
  }

  // NOTE: this is public so OrientationEventListenerImpl can access it.
  // Unfortunately, Java doesn't know about friendship.
  public void updateScreenOrientation() {
    int rotation = GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getRotation();
    short previousOrientation = mOrientation;

    if (rotation == Surface.ROTATION_0) {
      mOrientation = eScreenOrientation_PortraitPrimary;
    } else if (rotation == Surface.ROTATION_180) {
      mOrientation = eScreenOrientation_PortraitSecondary;
    } else if (rotation == Surface.ROTATION_270) {
      mOrientation = eScreenOrientation_LandscapeSecondary;
    } else if (rotation == Surface.ROTATION_90) {
      mOrientation = eScreenOrientation_LandscapePrimary;
    } else {
      Log.e("GeckoScreenOrientationListener", "Unexpected value received! (" + rotation + ")");
      return;
    }

    if (mShouldNotify && mOrientation != previousOrientation) {
      GeckoAppShell.sendEventToGecko(new GeckoEvent(mOrientation));
    }
  }

  public short getScreenOrientation() {
    return mOrientation;
  }
}
Loading