OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/content/display/screen_orientation_delegate_chromeos.h" | 5 #include "ash/content/display/screen_orientation_controller_chromeos.h" |
6 | 6 |
7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
8 #include "ash/display/display_info.h" | 8 #include "ash/display/display_info.h" |
9 #include "ash/display/display_manager.h" | 9 #include "ash/display/display_manager.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | 11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
12 #include "base/auto_reset.h" | |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "chromeos/accelerometer/accelerometer_reader.h" | |
13 #include "content/public/browser/screen_orientation_provider.h" | 15 #include "content/public/browser/screen_orientation_provider.h" |
14 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
15 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
16 #include "ui/gfx/display.h" | 18 #include "ui/gfx/display.h" |
17 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
23 // The angle which the screen has to be rotated past before the display will | |
24 // rotate to match it (i.e. 45.0f is no stickiness). | |
25 const float kDisplayRotationStickyAngleDegrees = 60.0f; | |
26 | |
27 // The minimum acceleration in m/s^2 in a direction required to trigger screen | |
28 // rotation. This prevents rapid toggling of rotation when the device is near | |
29 // flat and there is very little screen aligned force on it. The value is | |
30 // effectively the sine of the rise angle required times the acceleration due | |
31 // to gravity, with the current value requiring at least a 25 degree rise. | |
32 const float kMinimumAccelerationScreenRotation = 4.2f; | |
33 | |
21 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { | 34 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { |
22 ash::DisplayManager* display_manager = | 35 ash::DisplayManager* display_manager = |
23 ash::Shell::GetInstance()->display_manager(); | 36 ash::Shell::GetInstance()->display_manager(); |
24 if (!display_manager->HasInternalDisplay()) | 37 if (!display_manager->HasInternalDisplay()) |
25 return blink::WebScreenOrientationLockLandscape; | 38 return blink::WebScreenOrientationLockLandscape; |
26 | 39 |
27 ash::DisplayInfo info = | 40 ash::DisplayInfo info = |
28 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); | 41 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); |
29 gfx::Size size = info.size_in_pixel(); | 42 gfx::Size size = info.size_in_pixel(); |
30 switch (info.rotation()) { | 43 switch (info.rotation()) { |
31 case gfx::Display::ROTATE_0: | 44 case gfx::Display::ROTATE_0: |
32 case gfx::Display::ROTATE_180: | 45 case gfx::Display::ROTATE_180: |
33 return size.height() >= size.width() | 46 return size.height() >= size.width() |
34 ? blink::WebScreenOrientationLockPortrait | 47 ? blink::WebScreenOrientationLockPortrait |
35 : blink::WebScreenOrientationLockLandscape; | 48 : blink::WebScreenOrientationLockLandscape; |
36 case gfx::Display::ROTATE_90: | 49 case gfx::Display::ROTATE_90: |
37 case gfx::Display::ROTATE_270: | 50 case gfx::Display::ROTATE_270: |
38 return size.height() < size.width() | 51 return size.height() < size.width() |
39 ? blink::WebScreenOrientationLockPortrait | 52 ? blink::WebScreenOrientationLockPortrait |
40 : blink::WebScreenOrientationLockLandscape; | 53 : blink::WebScreenOrientationLockLandscape; |
41 } | 54 } |
42 NOTREACHED(); | 55 NOTREACHED(); |
43 return blink::WebScreenOrientationLockLandscape; | 56 return blink::WebScreenOrientationLockLandscape; |
44 } | 57 } |
45 | 58 |
46 } // namespace | 59 } // namespace |
47 | 60 |
48 namespace ash { | 61 namespace ash { |
49 | 62 |
50 ScreenOrientationDelegate::ScreenOrientationDelegate() | 63 ScreenOrientationController::ScreenOrientationController() |
51 : locking_window_(NULL), | 64 : locking_window_(NULL), |
52 natural_orientation_(GetDisplayNaturalOrientation()) { | 65 natural_orientation_(GetDisplayNaturalOrientation()), |
66 ignore_display_configuration_updates_(false), | |
67 rotation_locked_(false), | |
68 user_rotation_(gfx::Display::ROTATE_0), | |
69 current_rotation_(gfx::Display::ROTATE_0) { | |
53 content::ScreenOrientationProvider::SetDelegate(this); | 70 content::ScreenOrientationProvider::SetDelegate(this); |
71 Shell::GetInstance()->AddShellObserver(this); | |
54 } | 72 } |
55 | 73 |
56 ScreenOrientationDelegate::~ScreenOrientationDelegate() { | 74 ScreenOrientationController::~ScreenOrientationController() { |
57 content::ScreenOrientationProvider::SetDelegate(NULL); | 75 content::ScreenOrientationProvider::SetDelegate(NULL); |
76 Shell::GetInstance()->RemoveShellObserver(this); | |
77 Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); | |
78 Shell::GetInstance()->display_controller()->RemoveObserver(this); | |
58 } | 79 } |
59 | 80 |
60 bool ScreenOrientationDelegate::FullScreenRequired( | 81 void ScreenOrientationController::AddObserver(Observer* observer) { |
82 observers_.AddObserver(observer); | |
83 } | |
84 | |
85 void ScreenOrientationController::RemoveObserver(Observer* observer) { | |
86 observers_.RemoveObserver(observer); | |
87 } | |
88 | |
89 void ScreenOrientationController::SetRotationLocked(bool rotation_locked) { | |
90 if (rotation_locked_ == rotation_locked) | |
91 return; | |
92 rotation_locked_ = rotation_locked; | |
93 FOR_EACH_OBSERVER(Observer, observers_, | |
94 OnRotationLockChanged(rotation_locked_)); | |
95 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
96 if (!display_manager->HasInternalDisplay()) | |
97 return; | |
98 base::AutoReset<bool> auto_ignore_display_configuration_updates( | |
99 &ignore_display_configuration_updates_, true); | |
100 display_manager->RegisterDisplayRotationProperties(rotation_locked_, | |
101 current_rotation_); | |
102 } | |
103 | |
104 void ScreenOrientationController::SetDisplayRotation( | |
105 gfx::Display::Rotation rotation) { | |
106 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
107 if (!display_manager->HasInternalDisplay()) | |
108 return; | |
109 current_rotation_ = rotation; | |
110 base::AutoReset<bool> auto_ignore_display_configuration_updates( | |
111 &ignore_display_configuration_updates_, true); | |
112 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), | |
113 rotation); | |
114 } | |
115 | |
116 void ScreenOrientationController::OnAccelerometerUpdated( | |
117 const ui::AccelerometerUpdate& update) { | |
118 if (rotation_locked_) | |
119 return; | |
120 if (!update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) | |
121 return; | |
122 // Ignore the reading if it appears unstable. The reading is considered | |
123 // unstable if it deviates too much from gravity | |
124 if (!chromeos::AccelerometerReader::IsReadingStable( | |
125 update, ui::ACCELEROMETER_SOURCE_SCREEN)) { | |
126 return; | |
127 } | |
128 HandleScreenRotation(update.get(ui::ACCELEROMETER_SOURCE_SCREEN)); | |
oshima
2015/01/09 21:23:55
optional: or you can do
if (IsStable) {
HandleS
jonross
2015/01/12 14:46:49
Done.
| |
129 } | |
130 | |
131 bool ScreenOrientationController::FullScreenRequired( | |
61 content::WebContents* web_contents) { | 132 content::WebContents* web_contents) { |
62 return true; | 133 return true; |
63 } | 134 } |
64 | 135 |
65 void ScreenOrientationDelegate::Lock( | 136 void ScreenOrientationController::Lock( |
66 content::WebContents* web_contents, | 137 content::WebContents* web_contents, |
67 blink::WebScreenOrientationLockType lock_orientation) { | 138 blink::WebScreenOrientationLockType lock_orientation) { |
68 aura::Window* requesting_window = web_contents->GetNativeView(); | 139 aura::Window* requesting_window = web_contents->GetNativeView(); |
69 | |
70 // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation | |
71 // lock. Have MaximizeModeController, and TrayRotationLock both use it | |
72 // instead. | |
73 MaximizeModeController* controller = | |
74 Shell::GetInstance()->maximize_mode_controller(); | |
75 | |
76 // TODO(jonross): Track one rotation lock per window. When the active window | 140 // TODO(jonross): Track one rotation lock per window. When the active window |
77 // changes apply any corresponding rotation lock. | 141 // changes apply any corresponding rotation lock. |
78 if (!locking_window_) | 142 if (!locking_window_) |
79 locking_window_ = requesting_window; | 143 locking_window_ = requesting_window; |
80 else if (requesting_window != locking_window_) | 144 else if (requesting_window != locking_window_) |
81 return; | 145 return; |
82 | 146 |
83 switch (lock_orientation) { | 147 switch (lock_orientation) { |
84 case blink::WebScreenOrientationLockAny: | 148 case blink::WebScreenOrientationLockAny: |
85 controller->SetRotationLocked(false); | 149 SetRotationLocked(false); |
86 locking_window_ = NULL; | 150 locking_window_ = NULL; |
87 break; | 151 break; |
88 case blink::WebScreenOrientationLockDefault: | 152 case blink::WebScreenOrientationLockDefault: |
89 NOTREACHED(); | 153 NOTREACHED(); |
90 break; | 154 break; |
91 case blink::WebScreenOrientationLockPortraitPrimary: | 155 case blink::WebScreenOrientationLockPortraitPrimary: |
92 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); | 156 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); |
93 break; | 157 break; |
94 case blink::WebScreenOrientationLockLandscape: | 158 case blink::WebScreenOrientationLockLandscape: |
95 case blink::WebScreenOrientationLockPortrait: | 159 case blink::WebScreenOrientationLockPortrait: |
96 LockToRotationMatchingOrientation(lock_orientation); | 160 LockToRotationMatchingOrientation(lock_orientation); |
97 break; | 161 break; |
98 case blink::WebScreenOrientationLockPortraitSecondary: | 162 case blink::WebScreenOrientationLockPortraitSecondary: |
99 LockRotationToSecondaryOrientation( | 163 LockRotationToSecondaryOrientation( |
100 blink::WebScreenOrientationLockPortrait); | 164 blink::WebScreenOrientationLockPortrait); |
101 break; | 165 break; |
102 case blink::WebScreenOrientationLockLandscapeSecondary: | 166 case blink::WebScreenOrientationLockLandscapeSecondary: |
103 LockRotationToSecondaryOrientation( | 167 LockRotationToSecondaryOrientation( |
104 blink::WebScreenOrientationLockLandscape); | 168 blink::WebScreenOrientationLockLandscape); |
105 break; | 169 break; |
106 case blink::WebScreenOrientationLockLandscapePrimary: | 170 case blink::WebScreenOrientationLockLandscapePrimary: |
107 LockRotationToPrimaryOrientation( | 171 LockRotationToPrimaryOrientation( |
108 blink::WebScreenOrientationLockLandscape); | 172 blink::WebScreenOrientationLockLandscape); |
109 break; | 173 break; |
110 case blink::WebScreenOrientationLockNatural: | 174 case blink::WebScreenOrientationLockNatural: |
111 controller->LockRotation(gfx::Display::ROTATE_0); | 175 LockRotation(gfx::Display::ROTATE_0); |
112 break; | 176 break; |
113 default: | 177 default: |
114 NOTREACHED(); | 178 NOTREACHED(); |
115 break; | 179 break; |
116 } | 180 } |
117 } | 181 } |
118 | 182 |
119 bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { | 183 bool ScreenOrientationController::ScreenOrientationProviderSupported() { |
120 return Shell::GetInstance() | 184 return Shell::GetInstance() |
121 ->maximize_mode_controller() | 185 ->maximize_mode_controller() |
122 ->IsMaximizeModeWindowManagerEnabled(); | 186 ->IsMaximizeModeWindowManagerEnabled() && |
187 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
188 switches::kAshEnableTouchViewTesting); | |
123 } | 189 } |
124 | 190 |
125 void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { | 191 void ScreenOrientationController::Unlock(content::WebContents* web_contents) { |
126 aura::Window* requesting_window = web_contents->GetNativeView(); | 192 aura::Window* requesting_window = web_contents->GetNativeView(); |
127 if (requesting_window != locking_window_) | 193 if (requesting_window != locking_window_) |
128 return; | 194 return; |
129 locking_window_ = NULL; | 195 locking_window_ = NULL; |
130 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); | 196 SetRotationLocked(false); |
131 } | 197 } |
132 | 198 |
133 void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( | 199 void ScreenOrientationController::OnDisplayConfigurationChanged() { |
134 blink::WebScreenOrientationLockType lock_orientation) { | 200 if (ignore_display_configuration_updates_) |
135 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | 201 return; |
136 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 | 202 gfx::Display::Rotation user_rotation = |
137 : gfx::Display::ROTATE_90); | 203 Shell::GetInstance() |
204 ->display_manager() | |
205 ->GetDisplayInfo(gfx::Display::InternalDisplayId()) | |
206 .rotation(); | |
207 if (user_rotation != current_rotation_) { | |
208 // A user may change other display configuration settings. When the user | |
209 // does change the rotation setting, then lock rotation to prevent the | |
210 // accelerometer from erasing their change. | |
211 SetRotationLocked(true); | |
212 user_rotation_ = current_rotation_ = user_rotation; | |
213 } | |
138 } | 214 } |
139 | 215 |
140 void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( | 216 void ScreenOrientationController::OnMaximizeModeStarted() { |
141 blink::WebScreenOrientationLockType lock_orientation) { | 217 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
142 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | 218 if (!display_manager->HasInternalDisplay()) |
143 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 | 219 return; |
144 : gfx::Display::ROTATE_270); | 220 current_rotation_ = user_rotation_ = |
221 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) | |
222 .rotation(); | |
223 LoadDisplayRotationProperties(); | |
224 Shell::GetInstance()->accelerometer_reader()->AddObserver(this); | |
225 Shell::GetInstance()->display_controller()->AddObserver(this); | |
145 } | 226 } |
146 | 227 |
147 void ScreenOrientationDelegate::LockToRotationMatchingOrientation( | 228 void ScreenOrientationController::OnMaximizeModeEnded() { |
229 if (!Shell::GetInstance()->display_manager()->HasInternalDisplay()) | |
230 return; | |
231 Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); | |
232 Shell::GetInstance()->display_controller()->RemoveObserver(this); | |
233 if (current_rotation_ != user_rotation_) | |
234 SetDisplayRotation(user_rotation_); | |
235 } | |
236 | |
237 void ScreenOrientationController::LockRotation( | |
238 gfx::Display::Rotation rotation) { | |
239 SetRotationLocked(true); | |
240 SetDisplayRotation(rotation); | |
241 } | |
242 | |
243 void ScreenOrientationController::LockRotationToPrimaryOrientation( | |
244 blink::WebScreenOrientationLockType lock_orientation) { | |
245 LockRotation(natural_orientation_ == lock_orientation | |
246 ? gfx::Display::ROTATE_0 | |
247 : gfx::Display::ROTATE_90); | |
248 } | |
249 | |
250 void ScreenOrientationController::LockRotationToSecondaryOrientation( | |
251 blink::WebScreenOrientationLockType lock_orientation) { | |
252 LockRotation(natural_orientation_ == lock_orientation | |
253 ? gfx::Display::ROTATE_180 | |
254 : gfx::Display::ROTATE_270); | |
255 } | |
256 | |
257 void ScreenOrientationController::LockToRotationMatchingOrientation( | |
148 blink::WebScreenOrientationLockType lock_orientation) { | 258 blink::WebScreenOrientationLockType lock_orientation) { |
149 // TODO(jonross): Update MaximizeModeController to allow rotation between | 259 // TODO(jonross): Update MaximizeModeController to allow rotation between |
150 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from | 260 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from |
151 // ROTATE_90 to ROTATE_270) | 261 // ROTATE_90 to ROTATE_270) |
152 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 262 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
153 if (!display_manager->HasInternalDisplay()) | 263 if (!display_manager->HasInternalDisplay()) |
154 return; | 264 return; |
155 | 265 |
156 gfx::Display::Rotation rotation = | 266 gfx::Display::Rotation rotation = |
157 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) | 267 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
158 .rotation(); | 268 .rotation(); |
159 MaximizeModeController* controller = | |
160 Shell::GetInstance()->maximize_mode_controller(); | |
161 if (natural_orientation_ == lock_orientation) { | 269 if (natural_orientation_ == lock_orientation) { |
162 if (rotation == gfx::Display::ROTATE_0 || | 270 if (rotation == gfx::Display::ROTATE_0 || |
163 rotation == gfx::Display::ROTATE_180) { | 271 rotation == gfx::Display::ROTATE_180) { |
164 controller->SetRotationLocked(true); | 272 SetRotationLocked(true); |
165 } else { | 273 } else { |
166 controller->LockRotation(gfx::Display::ROTATE_0); | 274 LockRotation(gfx::Display::ROTATE_0); |
167 } | 275 } |
168 } else { | 276 } else { |
169 if (rotation == gfx::Display::ROTATE_90 || | 277 if (rotation == gfx::Display::ROTATE_90 || |
170 rotation == gfx::Display::ROTATE_270) { | 278 rotation == gfx::Display::ROTATE_270) { |
171 controller->SetRotationLocked(true); | 279 SetRotationLocked(true); |
172 } else { | 280 } else { |
173 controller->LockRotation(gfx::Display::ROTATE_90); | 281 LockRotation(gfx::Display::ROTATE_90); |
174 } | 282 } |
175 } | 283 } |
176 } | 284 } |
177 | 285 |
286 void ScreenOrientationController::HandleScreenRotation( | |
287 const gfx::Vector3dF& lid) { | |
288 gfx::Vector3dF lid_flattened(lid.x(), lid.y(), 0.0f); | |
289 float lid_flattened_length = lid_flattened.Length(); | |
290 // When the lid is close to being flat, don't change rotation as it is too | |
291 // sensitive to slight movements. | |
292 if (lid_flattened_length < kMinimumAccelerationScreenRotation) | |
293 return; | |
294 | |
295 // The reference vector is the angle of gravity when the device is rotated | |
296 // clockwise by 45 degrees. Computing the angle between this vector and | |
297 // gravity we can easily determine the expected display rotation. | |
298 static const gfx::Vector3dF rotation_reference(-1.0f, -1.0f, 0.0f); | |
299 | |
300 // Set the down vector to match the expected direction of gravity given the | |
301 // last configured rotation. This is used to enforce a stickiness that the | |
302 // user must overcome to rotate the display and prevents frequent rotations | |
303 // when holding the device near 45 degrees. | |
304 gfx::Vector3dF down(0.0f, 0.0f, 0.0f); | |
305 if (current_rotation_ == gfx::Display::ROTATE_0) | |
306 down.set_y(-1.0f); | |
307 else if (current_rotation_ == gfx::Display::ROTATE_90) | |
308 down.set_x(-1.0f); | |
309 else if (current_rotation_ == gfx::Display::ROTATE_180) | |
310 down.set_y(1.0f); | |
311 else | |
312 down.set_x(1.0f); | |
313 | |
314 // Don't rotate if the screen has not passed the threshold. | |
315 if (gfx::AngleBetweenVectorsInDegrees(down, lid_flattened) < | |
316 kDisplayRotationStickyAngleDegrees) { | |
317 return; | |
318 } | |
319 | |
320 float angle = gfx::ClockwiseAngleBetweenVectorsInDegrees( | |
321 rotation_reference, lid_flattened, gfx::Vector3dF(0.0f, 0.0f, -1.0f)); | |
322 | |
323 gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_90; | |
324 if (angle < 90.0f) | |
325 new_rotation = gfx::Display::ROTATE_0; | |
326 else if (angle < 180.0f) | |
327 new_rotation = gfx::Display::ROTATE_270; | |
328 else if (angle < 270.0f) | |
329 new_rotation = gfx::Display::ROTATE_180; | |
330 | |
331 if (new_rotation != current_rotation_) | |
332 SetDisplayRotation(new_rotation); | |
333 } | |
334 | |
335 void ScreenOrientationController::LoadDisplayRotationProperties() { | |
336 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
337 if (!display_manager->registered_internal_display_rotation_lock()) | |
338 return; | |
339 SetDisplayRotation(display_manager->registered_internal_display_rotation()); | |
340 SetRotationLocked(true); | |
341 } | |
342 | |
178 } // namespace ash | 343 } // namespace ash |
OLD | NEW |