1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/touch/touch_uma.h"
7 #include "ash/metrics/user_metrics_recorder.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/gfx/geometry/point_conversions.h"
20 #include <X11/extensions/XInput2.h>
26 struct WindowTouchDetails
{
27 // Move and start times of the touch points. The key is the touch-id.
28 std::map
<int, base::TimeDelta
> last_move_time_
;
29 std::map
<int, base::TimeDelta
> last_start_time_
;
31 // The first and last positions of the touch points.
32 std::map
<int, gfx::Point
> start_touch_position_
;
33 std::map
<int, gfx::Point
> last_touch_position_
;
35 // Last time-stamp of the last touch-end event.
36 base::TimeDelta last_release_time_
;
38 // Stores the time of the last touch released on this window (if there was a
39 // multi-touch gesture on the window, then this is the release-time of the
40 // last touch on the window).
41 base::TimeDelta last_mt_time_
;
44 DEFINE_OWNED_WINDOW_PROPERTY_KEY(WindowTouchDetails
,
49 DECLARE_WINDOW_PROPERTY_TYPE(WindowTouchDetails
*);
54 TouchUMA
* TouchUMA::GetInstance() {
55 return base::Singleton
<TouchUMA
>::get();
58 void TouchUMA::RecordGestureEvent(aura::Window
* target
,
59 const ui::GestureEvent
& event
) {
60 GestureActionType action
= FindGestureActionType(target
, event
);
61 RecordGestureAction(action
);
63 if (event
.type() == ui::ET_GESTURE_END
&&
64 event
.details().touch_points() == 2) {
65 WindowTouchDetails
* details
= target
->GetProperty(kWindowTouchDetails
);
67 LOG(ERROR
) << "Window received gesture events without receiving any touch"
71 details
->last_mt_time_
= event
.time_stamp();
75 void TouchUMA::RecordGestureAction(GestureActionType action
) {
76 if (action
== GESTURE_UNKNOWN
|| action
>= GESTURE_ACTION_COUNT
)
78 UMA_HISTOGRAM_ENUMERATION("Ash.GestureTarget", action
,
79 GESTURE_ACTION_COUNT
);
82 void TouchUMA::RecordTouchEvent(aura::Window
* target
,
83 const ui::TouchEvent
& event
) {
84 UMA_HISTOGRAM_CUSTOM_COUNTS(
86 static_cast<int>(std::max(event
.pointer_details().radius_x(),
87 event
.pointer_details().radius_y())),
90 UpdateTouchState(event
);
92 WindowTouchDetails
* details
= target
->GetProperty(kWindowTouchDetails
);
94 details
= new WindowTouchDetails
;
95 target
->SetProperty(kWindowTouchDetails
, details
);
98 // Record the location of the touch points.
99 const int kBucketCountForLocation
= 100;
100 const gfx::Rect bounds
= target
->GetRootWindow()->bounds();
101 const int bucket_size_x
= std::max(1,
102 bounds
.width() / kBucketCountForLocation
);
103 const int bucket_size_y
= std::max(1,
104 bounds
.height() / kBucketCountForLocation
);
106 gfx::Point position
= event
.root_location();
108 // Prefer raw event location (when available) over calibrated location.
109 if (event
.HasNativeEvent()) {
110 position
= ui::EventLocationFromNative(event
.native_event());
111 position
= gfx::ToFlooredPoint(
112 gfx::ScalePoint(position
, 1. / target
->layer()->device_scale_factor()));
115 position
.set_x(std::min(bounds
.width() - 1, std::max(0, position
.x())));
116 position
.set_y(std::min(bounds
.height() - 1, std::max(0, position
.y())));
118 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionX",
119 position
.x() / bucket_size_x
,
120 0, kBucketCountForLocation
, kBucketCountForLocation
+ 1);
121 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionY",
122 position
.y() / bucket_size_y
,
123 0, kBucketCountForLocation
, kBucketCountForLocation
+ 1);
125 if (event
.type() == ui::ET_TOUCH_PRESSED
) {
126 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
127 UMA_TOUCHSCREEN_TAP_DOWN
);
129 details
->last_start_time_
[event
.touch_id()] = event
.time_stamp();
130 details
->start_touch_position_
[event
.touch_id()] = event
.root_location();
131 details
->last_touch_position_
[event
.touch_id()] = event
.location();
133 if (details
->last_release_time_
.ToInternalValue()) {
134 // Measuring the interval between a touch-release and the next
135 // touch-start is probably less useful when doing multi-touch (e.g.
136 // gestures, or multi-touch friendly apps). So count this only if the user
137 // hasn't done any multi-touch during the last 30 seconds.
138 base::TimeDelta diff
= event
.time_stamp() - details
->last_mt_time_
;
139 if (diff
.InSeconds() > 30) {
140 base::TimeDelta gap
= event
.time_stamp() - details
->last_release_time_
;
141 UMA_HISTOGRAM_COUNTS_10000("Ash.TouchStartAfterEnd",
142 gap
.InMilliseconds());
146 // Record the number of touch-points currently active for the window.
147 const int kMaxTouchPoints
= 10;
148 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.ActiveTouchPoints",
149 details
->last_start_time_
.size(),
150 1, kMaxTouchPoints
, kMaxTouchPoints
+ 1);
151 } else if (event
.type() == ui::ET_TOUCH_RELEASED
) {
152 if (details
->last_start_time_
.count(event
.touch_id())) {
153 base::TimeDelta duration
= event
.time_stamp() -
154 details
->last_start_time_
[event
.touch_id()];
155 // Look for touches that were [almost] stationary for a long time.
156 const double kLongStationaryTouchDuration
= 10;
157 const int kLongStationaryTouchDistanceSquared
= 100;
158 if (duration
.InSecondsF() > kLongStationaryTouchDuration
) {
159 gfx::Vector2d distance
= event
.root_location() -
160 details
->start_touch_position_
[event
.touch_id()];
161 if (distance
.LengthSquared() < kLongStationaryTouchDistanceSquared
) {
162 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.StationaryTouchDuration",
163 duration
.InSeconds(),
164 kLongStationaryTouchDuration
,
170 details
->last_start_time_
.erase(event
.touch_id());
171 details
->last_move_time_
.erase(event
.touch_id());
172 details
->start_touch_position_
.erase(event
.touch_id());
173 details
->last_touch_position_
.erase(event
.touch_id());
174 details
->last_release_time_
= event
.time_stamp();
175 } else if (event
.type() == ui::ET_TOUCH_MOVED
) {
177 if (details
->last_touch_position_
.count(event
.touch_id())) {
178 gfx::Point lastpos
= details
->last_touch_position_
[event
.touch_id()];
180 std::abs(lastpos
.x() - event
.x()) + std::abs(lastpos
.y() - event
.y());
183 if (details
->last_move_time_
.count(event
.touch_id())) {
184 base::TimeDelta move_delay
= event
.time_stamp() -
185 details
->last_move_time_
[event
.touch_id()];
186 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveInterval",
187 move_delay
.InMilliseconds(),
191 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveSteps", distance
, 1, 1000, 50);
193 details
->last_move_time_
[event
.touch_id()] = event
.time_stamp();
194 details
->last_touch_position_
[event
.touch_id()] = event
.location();
199 : is_single_finger_gesture_(false),
200 touch_in_progress_(false),
204 TouchUMA::~TouchUMA() {
207 void TouchUMA::UpdateTouchState(const ui::TouchEvent
& event
) {
208 if (event
.type() == ui::ET_TOUCH_PRESSED
) {
209 if (!touch_in_progress_
) {
210 is_single_finger_gesture_
= true;
211 base::TimeDelta difference
= event
.time_stamp() - last_touch_down_time_
;
212 if (difference
> base::TimeDelta::FromMilliseconds(250)) {
214 UMA_HISTOGRAM_COUNTS_100("Ash.TouchStartBurst",
215 std::min(burst_length_
, 100));
222 is_single_finger_gesture_
= false;
224 touch_in_progress_
= true;
225 last_touch_down_time_
= event
.time_stamp();
226 } else if (event
.type() == ui::ET_TOUCH_RELEASED
) {
227 if (!aura::Env::GetInstance()->is_touch_down())
228 touch_in_progress_
= false;
232 TouchUMA::GestureActionType
TouchUMA::FindGestureActionType(
233 aura::Window
* window
,
234 const ui::GestureEvent
& event
) {
235 if (!window
|| window
->GetRootWindow() == window
) {
236 if (event
.type() == ui::ET_GESTURE_SCROLL_BEGIN
)
237 return GESTURE_BEZEL_SCROLL
;
238 if (event
.type() == ui::ET_GESTURE_BEGIN
)
239 return GESTURE_BEZEL_DOWN
;
240 return GESTURE_UNKNOWN
;
243 std::string name
= window
? window
->name() : std::string();
245 const char kDesktopBackgroundView
[] = "DesktopBackgroundView";
246 if (name
== kDesktopBackgroundView
) {
247 if (event
.type() == ui::ET_GESTURE_SCROLL_BEGIN
)
248 return GESTURE_DESKTOP_SCROLL
;
249 if (event
.type() == ui::ET_GESTURE_PINCH_BEGIN
)
250 return GESTURE_DESKTOP_PINCH
;
251 return GESTURE_UNKNOWN
;
254 const char kWebPage
[] = "RenderWidgetHostViewAura";
255 if (name
== kWebPage
) {
256 if (event
.type() == ui::ET_GESTURE_PINCH_BEGIN
)
257 return GESTURE_WEBPAGE_PINCH
;
258 if (event
.type() == ui::ET_GESTURE_SCROLL_BEGIN
)
259 return GESTURE_WEBPAGE_SCROLL
;
260 if (event
.type() == ui::ET_GESTURE_TAP
)
261 return GESTURE_WEBPAGE_TAP
;
262 return GESTURE_UNKNOWN
;
265 views::Widget
* widget
= views::Widget::GetWidgetForNativeView(window
);
267 return GESTURE_UNKNOWN
;
269 // |widget| may be in the process of destroying if it has ownership
270 // views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET and |event| was
271 // dispatched as part of gesture state cleanup. In this case the RootView
272 // of |widget| may no longer exist, so check before calling into any
274 if (!widget
->GetRootView())
275 return GESTURE_UNKNOWN
;
277 views::View
* view
= widget
->GetRootView()->
278 GetEventHandlerForPoint(event
.location());
280 return GESTURE_UNKNOWN
;
282 name
= view
->GetClassName();
284 const char kTabStrip
[] = "TabStrip";
285 const char kTab
[] = "BrowserTab";
286 if (name
== kTabStrip
|| name
== kTab
) {
287 if (event
.type() == ui::ET_GESTURE_SCROLL_BEGIN
)
288 return GESTURE_TABSTRIP_SCROLL
;
289 if (event
.type() == ui::ET_GESTURE_PINCH_BEGIN
)
290 return GESTURE_TABSTRIP_PINCH
;
291 if (event
.type() == ui::ET_GESTURE_TAP
)
292 return GESTURE_TABSTRIP_TAP
;
293 return GESTURE_UNKNOWN
;
296 const char kOmnibox
[] = "BrowserOmniboxViewViews";
297 if (name
== kOmnibox
) {
298 if (event
.type() == ui::ET_GESTURE_SCROLL_BEGIN
)
299 return GESTURE_OMNIBOX_SCROLL
;
300 if (event
.type() == ui::ET_GESTURE_PINCH_BEGIN
)
301 return GESTURE_OMNIBOX_PINCH
;
302 return GESTURE_UNKNOWN
;
305 return GESTURE_UNKNOWN
;