• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google LLC All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.skar.examples.helloskar.helpers;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.hardware.display.DisplayManager;
22 import android.hardware.display.DisplayManager.DisplayListener;
23 import android.view.Display;
24 import android.view.WindowManager;
25 
26 import com.google.ar.core.Session;
27 
28 /**
29  * Helper to track the display rotations. In particular, the 180 degree rotations are not notified
30  * by the onSurfaceChanged() callback, and thus they require listening to the android display
31  * events.
32  */
33 public final class DisplayRotationHelper implements DisplayListener {
34     private boolean viewportChanged;
35     private int viewportWidth;
36     private int viewportHeight;
37     private final Context context;
38     private final Display display;
39 
40     /**
41      * Constructs the DisplayRotationHelper but does not register the listener yet.
42      *
43      * @param context the Android {@link Context}.
44      */
DisplayRotationHelper(Context context)45     public DisplayRotationHelper(Context context) {
46         this.context = context;
47         display = context.getSystemService(WindowManager.class).getDefaultDisplay();
48     }
49 
50     /**
51      * Registers the display listener. Should be called from {@link Activity#onResume()}.
52      */
onResume()53     public void onResume() {
54         context.getSystemService(DisplayManager.class).registerDisplayListener(this, null);
55     }
56 
57     /**
58      * Unregisters the display listener. Should be called from {@link Activity#onPause()}.
59      */
onPause()60     public void onPause() {
61         context.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
62     }
63 
64     /**
65      * Records a change in surface dimensions. This will be later used by {@link
66      * #updateSessionIfNeeded(Session)}. Should be called from {@link
67      * android.opengl.GLSurfaceView.Renderer
68      * #onSurfaceChanged(javax.microedition.khronos.opengles.GL10, int, int)}.
69      *
70      * @param width  the updated width of the surface.
71      * @param height the updated height of the surface.
72      */
onSurfaceChanged(int width, int height)73     public void onSurfaceChanged(int width, int height) {
74         viewportWidth = width;
75         viewportHeight = height;
76         viewportChanged = true;
77     }
78 
79     /**
80      * Updates the session display geometry if a change was posted either by {@link
81      * #onSurfaceChanged(int, int)} call or by {@link #onDisplayChanged(int)} system callback. This
82      * function should be called explicitly before each call to {@link Session#update()}. This
83      * function will also clear the 'pending update' (viewportChanged) flag.
84      *
85      * @param session the {@link Session} object to update if display geometry changed.
86      */
updateSessionIfNeeded(Session session)87     public void updateSessionIfNeeded(Session session) {
88         if (viewportChanged) {
89             int displayRotation = display.getRotation();
90             session.setDisplayGeometry(displayRotation, viewportWidth, viewportHeight);
91             viewportChanged = false;
92         }
93     }
94 
95     /**
96      * Returns the current rotation state of android display. Same as {@link Display#getRotation()}.
97      */
getRotation()98     public int getRotation() {
99         return display.getRotation();
100     }
101 
102     @Override
onDisplayAdded(int displayId)103     public void onDisplayAdded(int displayId) {
104     }
105 
106     @Override
onDisplayRemoved(int displayId)107     public void onDisplayRemoved(int displayId) {
108     }
109 
110     @Override
onDisplayChanged(int displayId)111     public void onDisplayChanged(int displayId) {
112         viewportChanged = true;
113     }
114 }
115