File: motion_event_android.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 7,595 bytes parent folder | download | duplicates (5)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_EVENTS_ANDROID_MOTION_EVENT_ANDROID_H_
#define UI_EVENTS_ANDROID_MOTION_EVENT_ANDROID_H_

#include <jni.h>
#include <stddef.h>
#include <stdint.h>

#include <array>
#include <memory>

#include "base/android/scoped_java_ref.h"
#include "base/time/time.h"
#include "ui/events/events_export.h"
#include "ui/events/velocity_tracker/motion_event.h"
#include "ui/gfx/geometry/point_f.h"

namespace ui {

// An abstract base class which caches Android's MotionEvent object values and
// depends on impl classes for uncached data.
// All *input* coordinates are in device pixels (as with Android MotionEvent),
// while all *output* coordinates are in DIPs (as with WebTouchEvent).
class EVENTS_EXPORT MotionEventAndroid : public MotionEvent {
 public:
  // Returns the motion event action defined in Java layer for a given
  // MotionEvent::Action.
  static int GetAndroidAction(Action action);
  static int GetAndroidToolType(ToolType tool_type);

  struct Pointer {
    Pointer(jint id,
            jfloat pos_x_pixels,
            jfloat pos_y_pixels,
            jfloat touch_major_pixels,
            jfloat touch_minor_pixels,
            jfloat pressure,
            jfloat orientation_rad,
            jfloat tilt_rad,
            jint tool_type);
    jint id;
    jfloat pos_x_pixels;
    jfloat pos_y_pixels;
    jfloat touch_major_pixels;
    jfloat touch_minor_pixels;
    jfloat pressure;
    jfloat orientation_rad;
    // Unlike the tilt angles in motion_event.h, this field matches the
    // MotionEvent spec because we get this values from Java.
    jfloat tilt_rad;
    jint tool_type;
  };

  MotionEventAndroid(float pix_to_dip,
                     float ticks_x,
                     float ticks_y,
                     float tick_multiplier,
                     base::TimeTicks oldest_event_time,
                     base::TimeTicks latest_event_time,
                     base::TimeTicks cached_down_time_ms,
                     int android_action,
                     int pointer_count,
                     int history_size,
                     int action_index,
                     int android_action_button,
                     int android_gesture_classification,
                     int android_button_state,
                     int meta_state,
                     int source,
                     float raw_offset_x_pixels,
                     float raw_offset_y_pixels,
                     bool for_touch_handle,
                     const Pointer* const pointer0,
                     const Pointer* const pointer1);

  ~MotionEventAndroid() override;

  // Create a new instance from |this| with its cached pointers set
  // to a given point.
  virtual std::unique_ptr<MotionEventAndroid> CreateFor(
      const gfx::PointF& point) const;

  // Convenience method returning the pointer at index 0.
  gfx::PointF GetPoint() const { return gfx::PointF(GetX(0), GetY(0)); }
  gfx::PointF GetPointPix() const {
    return gfx::PointF(GetXPix(0), GetYPix(0));
  }

  // Start ui::MotionEvent overrides
  uint32_t GetUniqueEventId() const override;
  Action GetAction() const override;
  int GetActionIndex() const override;
  size_t GetPointerCount() const override;
  float GetRawX(size_t pointer_index) const override;
  float GetRawY(size_t pointer_index) const override;
  float GetTwist(size_t pointer_index) const override;
  float GetTangentialPressure(size_t pointer_index) const override;
  // TODO(crbug.com/41493853): Cleanup GetEventTime method to have same
  // semantics as Android side of MotionEvent.GetEventTime(). On Android side
  // GetEventTime() gives timestamp of the most recent input event, while in
  // chromium it gives timestamp of the oldest input event for batched inputs.
  base::TimeTicks GetEventTime() const override;
  base::TimeTicks GetLatestEventTime() const override;
  base::TimeTicks GetDownTime() const override;
  size_t GetHistorySize() const override;
  int GetSourceDeviceId(size_t pointer_index) const override;
  int GetButtonState() const override;
  int GetFlags() const override;
  Classification GetClassification() const override;
  // End ui::MotionEvent overrides

  int GetActionButton() const;
  int GetSource() const;
  float ticks_x() const { return ticks_x_; }
  float ticks_y() const { return ticks_y_; }
  float GetTickMultiplier() const;
  bool for_touch_handle() const { return for_touch_handle_; }

  float GetRawXPix(size_t pointer_index) const;
  virtual float GetXPix(size_t pointer_index) const = 0;
  virtual float GetYPix(size_t pointer_index) const = 0;

  virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() const;

 protected:
  float pix_to_dip() const { return pix_to_dip_; }
  float ToDips(float pixels) const;

  // Cache pointer coords, id's and major lengths for the most common
  // touch-related scenarios, i.e., scrolling and pinching.  This prevents
  // redundant JNI fetches for the same bits.
  enum { MAX_POINTERS_TO_CACHE = 2 };

  MotionEventAndroid(const MotionEventAndroid& e, const gfx::PointF& point);

  struct CachedPointer {
    CachedPointer();
    int id = 0;
    gfx::PointF position;
    float touch_major = 0;
    float touch_minor = 0;
    float pressure = 0;
    float orientation = 0;
    float tilt_x = 0;
    float tilt_y = 0;
    ToolType tool_type = ToolType::UNKNOWN;
  };

  std::array<CachedPointer, MAX_POINTERS_TO_CACHE> cached_pointers_;

  static ToolType FromAndroidToolType(int android_tool_type);
  static base::TimeTicks FromAndroidTime(base::TimeTicks time);
  static float ToValidFloat(float x);
  static void ConvertTiltOrientationToTiltXY(float tilt_rad,
                                             float orientation_rad,
                                             float* tilt_x,
                                             float* tilt_y);

 private:
  CachedPointer FromAndroidPointer(const Pointer& pointer) const;
  CachedPointer CreateCachedPointer(const CachedPointer& pointer,
                                    const gfx::PointF& point) const;

  // Used to convert pixel coordinates from the Java-backed MotionEvent to
  // DIP coordinates cached/returned by the MotionEventAndroid.
  const float pix_to_dip_;

  // Variables for mouse wheel event.
  const float ticks_x_;
  const float ticks_y_;
  const float tick_multiplier_;
  const int source_;

  const bool for_touch_handle_;

  // |cached_oldest_event_time_| and |cached_latest_event_time_| are same when
  // history size is 0, in presence of historical events
  // |cached_oldest_event_time_| is the event time of oldest coalesced event.
  const base::TimeTicks cached_oldest_event_time_;
  const base::TimeTicks cached_latest_event_time_;
  // This stores the event time of first down event in touch sequence, it is
  // obtained from MotionEvent.getDownTime for java backed events and
  // from AMotionEvent_getDowntime for native backed events.
  const base::TimeTicks cached_down_time_ms_;
  const Action cached_action_;
  const size_t cached_pointer_count_;
  const size_t cached_history_size_;
  const int cached_action_index_;
  const int cached_action_button_;
  const int cached_gesture_classification_;
  const int cached_button_state_;
  const int cached_flags_;
  const gfx::Vector2dF cached_raw_position_offset_;

  // A unique identifier for the Android motion event.
  const uint32_t unique_event_id_;
};

}  // namespace ui

#endif  // UI_EVENTS_ANDROID_MOTION_EVENT_ANDROID_H_