summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenChangeListener.java
blob: 78d66cc35224a974794e28ef4ee0f0f89088c805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* -*- Mode: Java; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
 * 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.hardware.display.DisplayManager;
import android.util.Log;
import android.view.Display;

public class GeckoScreenChangeListener implements DisplayManager.DisplayListener {
  private static final String LOGTAG = "ScreenChangeListener";
  private static final boolean DEBUG = false;

  public GeckoScreenChangeListener() {}

  @Override
  public void onDisplayAdded(final int displayId) {}

  @Override
  public void onDisplayRemoved(final int displayId) {}

  @Override
  public void onDisplayChanged(final int displayId) {
    if (DEBUG) {
      Log.d(LOGTAG, "onDisplayChanged");
    }

    // Even if onDisplayChanged is called, Configuration may not updated yet.
    // So we use Display's data instead.
    if (displayId != Display.DEFAULT_DISPLAY) {
      if (DEBUG) {
        Log.d(LOGTAG, "Primary display is only supported");
      }
      return;
    }

    final DisplayManager displayManager = getDisplayManager();
    if (displayManager == null) {
      return;
    }

    if (GeckoScreenOrientation.getInstance().update(displayManager.getDisplay(displayId))) {
      // refreshScreenInfo is already called.
      return;
    }

    ScreenManagerHelper.refreshScreenInfo();
  }

  private static DisplayManager getDisplayManager() {
    return (DisplayManager)
        GeckoAppShell.getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);
  }

  public void enable() {
    final DisplayManager displayManager = getDisplayManager();
    if (displayManager == null) {
      return;
    }
    displayManager.registerDisplayListener(this, null);
  }

  public void disable() {
    final DisplayManager displayManager = getDisplayManager();
    if (displayManager == null) {
      return;
    }
    displayManager.unregisterDisplayListener(this);
  }
}