15
15
import android .os .HandlerThread ;
16
16
import android .view .IDisplayWindowListener ;
17
17
18
+ import java .util .Objects ;
19
+
18
20
public class DisplaySizeMonitor {
19
21
22
+ private static class SessionInfo {
23
+ private Size size ;
24
+ private int rotation ;
25
+
26
+ public SessionInfo (Size size , int rotation ) {
27
+ this .size = size ;
28
+ this .rotation = rotation ;
29
+ }
30
+
31
+ @ Override
32
+ public boolean equals (Object o ) {
33
+ if (this == o )
34
+ return true ;
35
+ if (!(o instanceof SessionInfo ))
36
+ return false ;
37
+ SessionInfo that = (SessionInfo ) o ;
38
+ return rotation == that .rotation && Objects .equals (size , that .size );
39
+ }
40
+
41
+ @ Override
42
+ public String toString () {
43
+ return "{" + "size=" + size + ", rotation=" + rotation + '}' ;
44
+ }
45
+ }
46
+
20
47
public interface Listener {
21
48
void onDisplaySizeChanged ();
22
49
}
@@ -34,7 +61,7 @@ public interface Listener {
34
61
35
62
private int displayId = Device .DISPLAY_ID_NONE ;
36
63
37
- private Size sessionDisplaySize ;
64
+ private SessionInfo sessionInfo ;
38
65
39
66
private Listener listener ;
40
67
@@ -98,12 +125,16 @@ public void stopAndRelease() {
98
125
}
99
126
}
100
127
101
- private synchronized Size getSessionDisplaySize () {
102
- return sessionDisplaySize ;
128
+ private synchronized SessionInfo getSessionInfo () {
129
+ return sessionInfo ;
130
+ }
131
+
132
+ private synchronized void setSessionInfo (SessionInfo sessionInfo ) {
133
+ this .sessionInfo = sessionInfo ;
103
134
}
104
135
105
- public synchronized void setSessionDisplaySize (Size sessionDisplaySize ) {
106
- this . sessionDisplaySize = sessionDisplaySize ;
136
+ public void setSessionInfo (Size size , int rotation ) {
137
+ setSessionInfo ( new SessionInfo ( size , rotation )) ;
107
138
}
108
139
109
140
private void checkDisplaySizeChanged () {
@@ -112,29 +143,29 @@ private void checkDisplaySizeChanged() {
112
143
Ln .w ("DisplayInfo for " + displayId + " cannot be retrieved" );
113
144
// We can't compare with the current size, so reset unconditionally
114
145
if (Ln .isEnabled (Ln .Level .VERBOSE )) {
115
- Ln .v ("DisplaySizeMonitor: requestReset(): " + getSessionDisplaySize () + " -> (unknown)" );
146
+ Ln .v ("DisplaySizeMonitor: requestReset(): " + getSessionInfo () + " -> (unknown)" );
116
147
}
117
- setSessionDisplaySize (null );
148
+ setSessionInfo (null );
118
149
listener .onDisplaySizeChanged ();
119
150
} else {
120
- Size size = di .getSize ();
151
+ SessionInfo si = new SessionInfo ( di .getSize (), di . getRotation () );
121
152
122
153
// The field is hidden on purpose, to read it with synchronization
123
154
@ SuppressWarnings ("checkstyle:HiddenField" )
124
- Size sessionDisplaySize = getSessionDisplaySize (); // synchronized
155
+ SessionInfo sessionInfo = getSessionInfo (); // synchronized
125
156
126
157
// .equals() also works if sessionDisplaySize == null
127
- if (!size .equals (sessionDisplaySize )) {
158
+ if (!si .equals (sessionInfo )) {
128
159
// Reset only if the size is different
129
160
if (Ln .isEnabled (Ln .Level .VERBOSE )) {
130
- Ln .v ("DisplaySizeMonitor: requestReset(): " + sessionDisplaySize + " -> " + size );
161
+ Ln .v ("DisplaySizeMonitor: requestReset(): " + sessionInfo + " -> " + si );
131
162
}
132
163
// Set the new size immediately, so that a future onDisplayChanged() event called before the asynchronous prepare()
133
164
// considers that the current size is the requested size (to avoid a duplicate requestReset())
134
- setSessionDisplaySize ( size );
165
+ setSessionInfo ( si );
135
166
listener .onDisplaySizeChanged ();
136
167
} else if (Ln .isEnabled (Ln .Level .VERBOSE )) {
137
- Ln .v ("DisplaySizeMonitor: Size not changed (" + size + "): do not requestReset()" );
168
+ Ln .v ("DisplaySizeMonitor: Size and rotation not changed (" + sessionInfo + "): do not requestReset()" );
138
169
}
139
170
}
140
171
}
0 commit comments