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 "chrome/browser/ui/android/autofill/autofill_popup_view_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/ui/android/window_android_helper.h"
10 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
11 #include "content/public/browser/android/content_view_core.h"
12 #include "jni/AutofillPopupBridge_jni.h"
13 #include "ui/base/android/view_android.h"
14 #include "ui/base/android/window_android.h"
15 #include "ui/gfx/rect.h"
19 AutofillPopupViewAndroid::AutofillPopupViewAndroid(
20 AutofillPopupController* controller)
21 : controller_(controller) {}
23 AutofillPopupViewAndroid::~AutofillPopupViewAndroid() {}
25 void AutofillPopupViewAndroid::Show() {
26 JNIEnv* env = base::android::AttachCurrentThread();
27 ui::ViewAndroid* view_android = controller_->container_view();
31 java_object_.Reset(Java_AutofillPopupBridge_create(
33 reinterpret_cast<intptr_t>(this),
34 view_android->GetWindowAndroid()->GetJavaObject().obj(),
35 view_android->GetJavaObject().obj()));
37 UpdateBoundsAndRedrawPopup();
40 void AutofillPopupViewAndroid::Hide() {
42 JNIEnv* env = base::android::AttachCurrentThread();
43 Java_AutofillPopupBridge_hide(env, java_object_.obj());
46 void AutofillPopupViewAndroid::UpdateBoundsAndRedrawPopup() {
47 JNIEnv* env = base::android::AttachCurrentThread();
48 Java_AutofillPopupBridge_setAnchorRect(
51 controller_->element_bounds().x(),
52 controller_->element_bounds().y(),
53 controller_->element_bounds().width(),
54 controller_->element_bounds().height());
56 // We need an array of AutofillSuggestion.
57 size_t count = controller_->names().size();
59 ScopedJavaLocalRef<jobjectArray> data_array =
60 Java_AutofillPopupBridge_createAutofillSuggestionArray(env, count);
62 for (size_t i = 0; i < count; ++i) {
63 ScopedJavaLocalRef<jstring> name =
64 base::android::ConvertUTF16ToJavaString(env, controller_->names()[i]);
65 ScopedJavaLocalRef<jstring> subtext =
66 base::android::ConvertUTF16ToJavaString(env,
67 controller_->subtexts()[i]);
68 Java_AutofillPopupBridge_addToAutofillSuggestionArray(
74 controller_->identifiers()[i]);
77 Java_AutofillPopupBridge_show(
78 env, java_object_.obj(), data_array.obj(), controller_->IsRTL());
81 void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv* env,
84 controller_->AcceptSuggestion(list_index);
87 void AutofillPopupViewAndroid::PopupDismissed(JNIEnv* env, jobject obj) {
89 controller_->ViewDestroyed();
94 void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
97 bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv* env) {
98 return RegisterNativesImpl(env);
102 AutofillPopupView* AutofillPopupView::Create(
103 AutofillPopupController* controller) {
104 return new AutofillPopupViewAndroid(controller);
107 } // namespace autofill