| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/android/view_android.h" | 5 #include "ui/android/view_android.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
| 11 #include "jni/ViewAndroidDelegate_jni.h" | 11 #include "jni/ViewAndroidDelegate_jni.h" |
| 12 #include "ui/android/view_client.h" |
| 12 #include "ui/android/window_android.h" | 13 #include "ui/android/window_android.h" |
| 13 #include "ui/display/display.h" | 14 #include "ui/display/display.h" |
| 14 #include "ui/display/screen.h" | 15 #include "ui/display/screen.h" |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 | 18 |
| 18 using base::android::JavaRef; | 19 using base::android::JavaRef; |
| 19 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
| 20 | 21 |
| 22 // ViewAndroid::ScopedAndroidView |
| 21 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 23 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 22 JNIEnv* env, | 24 JNIEnv* env, |
| 23 const JavaRef<jobject>& jview, | 25 const JavaRef<jobject>& jview, |
| 24 const JavaRef<jobject>& jdelegate) | 26 const JavaRef<jobject>& jdelegate) |
| 25 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { | 27 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { |
| 26 // If there's a view, then we need a delegate to remove it. | 28 // If there's a view, then we need a delegate to remove it. |
| 27 DCHECK(!jdelegate.is_null() || jview.is_null()); | 29 DCHECK(!jdelegate.is_null() || jview.is_null()); |
| 28 } | 30 } |
| 29 | 31 |
| 30 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } | 32 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 61 view_.reset(); | 63 view_.reset(); |
| 62 delegate_.reset(); | 64 delegate_.reset(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 const base::android::ScopedJavaLocalRef<jobject> | 67 const base::android::ScopedJavaLocalRef<jobject> |
| 66 ViewAndroid::ScopedAnchorView::view() const { | 68 ViewAndroid::ScopedAnchorView::view() const { |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | 69 JNIEnv* env = base::android::AttachCurrentThread(); |
| 68 return view_.get(env); | 70 return view_.get(env); |
| 69 } | 71 } |
| 70 | 72 |
| 71 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 73 // ViewAndroid::Bounds |
| 72 : parent_(nullptr) | 74 void ViewAndroid::Bounds::SetBounds(const gfx::Point& origin, |
| 73 , delegate_(base::android::AttachCurrentThread(), | 75 int width, |
| 74 delegate.obj()) {} | 76 int height) { |
| 77 rect_.SetRect(origin.x(), origin.y(), width, height); |
| 78 } |
| 75 | 79 |
| 76 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 80 bool ViewAndroid::Bounds::IsInBounds(const MotionEventAndroid& event) { |
| 81 bool width_matched = (rect_.width() == Bounds::kMatchParent) ? true : |
| 82 rect_.x() <= event.GetX(0) && event.GetX(0) < rect_.right(); |
| 83 bool height_matched = (rect_.height() == Bounds::kMatchParent) ? true: |
| 84 rect_.y() <= event.GetY(0) && event.GetY(0) < rect_.bottom(); |
| 85 return width_matched && height_matched; |
| 86 } |
| 87 |
| 88 // ViewAndroid |
| 89 ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr), |
| 90 client_(client) {} |
| 91 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| 77 | 92 |
| 78 ViewAndroid::~ViewAndroid() { | 93 ViewAndroid::~ViewAndroid() { |
| 79 RemoveFromParent(); | 94 RemoveFromParent(); |
| 80 | 95 |
| 81 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 96 for (auto& child : children_) { |
| 82 it != children_.end(); it++) { | 97 DCHECK_EQ(child->parent_, this); |
| 83 DCHECK_EQ((*it)->parent_, this); | 98 child->parent_ = nullptr; |
| 84 (*it)->parent_ = nullptr; | |
| 85 } | 99 } |
| 86 } | 100 } |
| 87 | 101 |
| 88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 102 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 103 // A ViewAndroid may have its own delegate or otherwise will |
| 104 // use the next available parent's delegate. |
| 89 JNIEnv* env = base::android::AttachCurrentThread(); | 105 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 106 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 91 } | 107 } |
| 92 | 108 |
| 93 void ViewAndroid::AddChild(ViewAndroid* child) { | 109 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 94 DCHECK(child); | 110 DCHECK(child); |
| 111 DCHECK(!child->IsViewRoot()); // ViewRoot cannot be a child. |
| 95 DCHECK(std::find(children_.begin(), children_.end(), child) == | 112 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 96 children_.end()); | 113 children_.end()); |
| 97 | 114 |
| 98 children_.push_back(child); | 115 children_.push_front(child); |
| 99 if (child->parent_) | 116 if (child->parent_) |
| 100 child->RemoveFromParent(); | 117 child->RemoveFromParent(); |
| 101 child->parent_ = this; | 118 child->parent_ = this; |
| 102 } | 119 } |
| 103 | 120 |
| 104 void ViewAndroid::RemoveFromParent() { | 121 void ViewAndroid::RemoveFromParent() { |
| 105 if (parent_) | 122 if (parent_) |
| 106 parent_->RemoveChild(this); | 123 parent_->RemoveChild(this); |
| 107 } | 124 } |
| 108 | 125 |
| 126 void ViewAndroid::SetBounds(const gfx::Point& origin, int width, int height) { |
| 127 bounds_.SetBounds(origin, width, height); |
| 128 } |
| 129 |
| 130 bool ViewAndroid::IsInBounds(const MotionEventAndroid& event) { |
| 131 return bounds_.IsInBounds(event); |
| 132 } |
| 133 |
| 109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 134 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 135 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 111 if (delegate.is_null()) | 136 if (delegate.is_null()) |
| 112 return ViewAndroid::ScopedAnchorView(); | 137 return ViewAndroid::ScopedAnchorView(); |
| 113 | 138 |
| 114 JNIEnv* env = base::android::AttachCurrentThread(); | 139 JNIEnv* env = base::android::AttachCurrentThread(); |
| 115 return ViewAndroid::ScopedAnchorView( | 140 return ViewAndroid::ScopedAnchorView( |
| 116 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); | 141 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); |
| 117 } | 142 } |
| 118 | 143 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 146 DCHECK(child); | 171 DCHECK(child); |
| 147 DCHECK_EQ(child->parent_, this); | 172 DCHECK_EQ(child->parent_, this); |
| 148 | 173 |
| 149 std::list<ViewAndroid*>::iterator it = | 174 std::list<ViewAndroid*>::iterator it = |
| 150 std::find(children_.begin(), children_.end(), child); | 175 std::find(children_.begin(), children_.end(), child); |
| 151 DCHECK(it != children_.end()); | 176 DCHECK(it != children_.end()); |
| 152 children_.erase(it); | 177 children_.erase(it); |
| 153 child->parent_ = nullptr; | 178 child->parent_ = nullptr; |
| 154 } | 179 } |
| 155 | 180 |
| 181 |
| 156 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 182 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
| 157 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 183 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
| 158 } | 184 } |
| 159 | 185 |
| 160 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() | 186 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() |
| 161 const { | 187 const { |
| 162 JNIEnv* env = base::android::AttachCurrentThread(); | 188 JNIEnv* env = base::android::AttachCurrentThread(); |
| 163 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); | 189 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
| 164 if (!delegate.is_null()) | 190 if (!delegate.is_null()) |
| 165 return delegate; | 191 return delegate; |
| 166 | 192 |
| 167 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; | 193 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; |
| 168 } | 194 } |
| 169 | 195 |
| 170 cc::Layer* ViewAndroid::GetLayer() const { | 196 cc::Layer* ViewAndroid::GetLayer() const { |
| 171 return layer_.get(); | 197 return layer_.get(); |
| 172 } | 198 } |
| 173 | 199 |
| 174 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 200 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 175 layer_ = layer; | 201 layer_ = layer; |
| 176 } | 202 } |
| 177 | 203 |
| 204 ViewAndroid* ViewAndroid::GetViewRoot() { |
| 205 return parent_ ? parent_->GetViewRoot() : nullptr; |
| 206 } |
| 207 |
| 208 bool ViewAndroid::IsViewRoot() { |
| 209 return GetViewRoot() == this; |
| 210 } |
| 211 |
| 178 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 212 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 179 const JavaRef<jobject>& jimage) { | 213 const JavaRef<jobject>& jimage) { |
| 180 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 214 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 181 if (delegate.is_null()) | 215 if (delegate.is_null()) |
| 182 return false; | 216 return false; |
| 183 JNIEnv* env = base::android::AttachCurrentThread(); | 217 JNIEnv* env = base::android::AttachCurrentThread(); |
| 184 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 218 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 185 jimage); | 219 jimage); |
| 186 } | 220 } |
| 187 | 221 |
| 222 bool ViewAndroid::OnTouchEventInternal(const MotionEventAndroid& event, |
| 223 bool is_touch_handle_event) { |
| 224 if (!IsInBounds(event)) |
| 225 return false; |
| 226 |
| 227 if (client_) { |
| 228 if (client_->OnTouchEvent(event, is_touch_handle_event)) |
| 229 return true; |
| 230 } |
| 231 |
| 232 for (auto& child: children_) { |
| 233 if (child->OnTouchEventInternal(event, is_touch_handle_event)) |
| 234 return true; |
| 235 } |
| 236 return false; |
| 237 } |
| 238 |
| 188 } // namespace ui | 239 } // namespace ui |
| OLD | NEW |