summaryrefslogtreecommitdiffstats
path: root/mojo/services/native_viewport/native_viewport_android.cc
blob: de240847731799c702bdae0e1ade74b52a50fabb (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/services/native_viewport/native_viewport_android.h"

#include <android/input.h>
#include <android/native_window_jni.h>

#include "base/android/jni_android.h"
#include "jni/NativeViewportAndroid_jni.h"
#include "mojo/shell/context.h"
#include "ui/events/event.h"
#include "ui/gfx/point.h"

namespace mojo {
namespace services {

ui::EventType MotionEventActionToEventType(jint action) {
  switch (action) {
    case AMOTION_EVENT_ACTION_DOWN:
      return ui::ET_TOUCH_PRESSED;
    case AMOTION_EVENT_ACTION_MOVE:
      return ui::ET_TOUCH_MOVED;
    case AMOTION_EVENT_ACTION_UP:
      return ui::ET_TOUCH_RELEASED;
    default:
      NOTREACHED();
  }
  return ui::ET_UNKNOWN;
}

////////////////////////////////////////////////////////////////////////////////
// NativeViewportAndroid, public:

// static
bool NativeViewportAndroid::Register(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

NativeViewportAndroid::NativeViewportAndroid(shell::Context* context,
                                             NativeViewportDelegate* delegate)
    : delegate_(delegate),
      context_(context),
      window_(NULL),
      id_generator_(0),
      weak_factory_(this) {
}

NativeViewportAndroid::~NativeViewportAndroid() {
  if (window_)
    ReleaseWindow();
}

void NativeViewportAndroid::Destroy(JNIEnv* env, jobject obj) {
  delegate_->OnDestroyed();
}

void NativeViewportAndroid::SurfaceCreated(JNIEnv* env,
                                           jobject obj,
                                           jobject jsurface) {
  base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface);
  window_ = ANativeWindow_fromSurface(env, jsurface);
  delegate_->OnAcceleratedWidgetAvailable(window_);
}

void NativeViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
  DCHECK(!window_);
  ReleaseWindow();
}

void NativeViewportAndroid::SurfaceSetSize(JNIEnv* env, jobject obj,
                                           jint width, jint height) {
  size_ = gfx::Size(width, height);
  delegate_->OnResized(size_);
}

bool NativeViewportAndroid::TouchEvent(JNIEnv* env, jobject obj,
                                       jint pointer_id,
                                       jint action,
                                       jfloat x, jfloat y,
                                       jlong time_ms) {
  gfx::Point location(static_cast<int>(x), static_cast<int>(y));
  ui::TouchEvent event(MotionEventActionToEventType(action), location,
                       id_generator_.GetGeneratedID(pointer_id),
                       base::TimeDelta::FromMilliseconds(time_ms));
  // TODO(beng): handle multiple touch-points.
  delegate_->OnEvent(&event);
  if (action == ui::ET_TOUCH_RELEASED)
    id_generator_.ReleaseNumber(pointer_id);

  return true;
}

////////////////////////////////////////////////////////////////////////////////
// NativeViewportAndroid, NativeViewport implementation:

void NativeViewportAndroid::Init() {
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_NativeViewportAndroid_createForActivity(env, context_->activity(),
                                               reinterpret_cast<jint>(this));
}

void NativeViewportAndroid::Close() {
  // TODO(beng): close activity containing MojoView?

  // TODO(beng): perform this in response to view destruction.
  delegate_->OnDestroyed();
}

gfx::Size NativeViewportAndroid::GetSize() {
  return size_;
}

void NativeViewportAndroid::SetCapture() {
  NOTIMPLEMENTED();
}

void NativeViewportAndroid::ReleaseCapture() {
  NOTIMPLEMENTED();
}

////////////////////////////////////////////////////////////////////////////////
// NativeViewportAndroid, private:

void NativeViewportAndroid::ReleaseWindow() {
  ANativeWindow_release(window_);
  window_ = NULL;
}

////////////////////////////////////////////////////////////////////////////////
// NativeViewport, public:

// static
scoped_ptr<NativeViewport> NativeViewport::Create(
    shell::Context* context,
    NativeViewportDelegate* delegate) {
  return scoped_ptr<NativeViewport>(
      new NativeViewportAndroid(context, delegate)).Pass();
}

}  // namespace services
}  // namespace mojo