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/android/resource_mapper.h"
10 #include "chrome/browser/ui/android/window_android_helper.h"
11 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
12 #include "components/autofill/core/browser/suggestion.h"
13 #include "content/public/browser/android/content_view_core.h"
14 #include "jni/AutofillPopupBridge_jni.h"
15 #include "ui/android/view_android.h"
16 #include "ui/android/window_android.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/android/java_bitmap.h"
19 #include "ui/gfx/geometry/rect.h"
23 AutofillPopupViewAndroid::AutofillPopupViewAndroid(
24 AutofillPopupController
* controller
)
25 : controller_(controller
) {}
27 AutofillPopupViewAndroid::~AutofillPopupViewAndroid() {}
29 void AutofillPopupViewAndroid::Show() {
30 JNIEnv
* env
= base::android::AttachCurrentThread();
31 ui::ViewAndroid
* view_android
= controller_
->container_view();
35 java_object_
.Reset(Java_AutofillPopupBridge_create(
37 reinterpret_cast<intptr_t>(this),
38 view_android
->GetWindowAndroid()->GetJavaObject().obj(),
39 view_android
->GetJavaObject().obj()));
41 UpdateBoundsAndRedrawPopup();
44 void AutofillPopupViewAndroid::Hide() {
46 JNIEnv
* env
= base::android::AttachCurrentThread();
47 Java_AutofillPopupBridge_dismiss(env
, java_object_
.obj());
50 void AutofillPopupViewAndroid::UpdateBoundsAndRedrawPopup() {
51 JNIEnv
* env
= base::android::AttachCurrentThread();
52 Java_AutofillPopupBridge_setAnchorRect(
55 controller_
->element_bounds().x(),
56 controller_
->element_bounds().y(),
57 controller_
->element_bounds().width(),
58 controller_
->element_bounds().height());
60 size_t count
= controller_
->GetLineCount();
61 ScopedJavaLocalRef
<jobjectArray
> data_array
=
62 Java_AutofillPopupBridge_createAutofillSuggestionArray(env
, count
);
64 for (size_t i
= 0; i
< count
; ++i
) {
65 ScopedJavaLocalRef
<jstring
> value
= base::android::ConvertUTF16ToJavaString(
66 env
, controller_
->GetElidedValueAt(i
));
67 ScopedJavaLocalRef
<jstring
> label
=
68 base::android::ConvertUTF16ToJavaString(
69 env
, controller_
->GetElidedLabelAt(i
));
70 int android_icon_id
= 0;
72 const autofill::Suggestion
& suggestion
= controller_
->GetSuggestionAt(i
);
73 if (!suggestion
.icon
.empty()) {
74 android_icon_id
= ResourceMapper::MapFromChromiumId(
75 controller_
->GetIconResourceID(suggestion
.icon
));
78 Java_AutofillPopupBridge_addToAutofillSuggestionArray(
85 suggestion
.frontend_id
);
88 Java_AutofillPopupBridge_show(
89 env
, java_object_
.obj(), data_array
.obj(), controller_
->IsRTL());
92 void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv
* env
,
95 // Race: Hide() may have already run.
97 controller_
->AcceptSuggestion(list_index
);
100 void AutofillPopupViewAndroid::PopupDismissed(JNIEnv
* env
, jobject obj
) {
102 controller_
->ViewDestroyed();
107 void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
110 bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv
* env
) {
111 return RegisterNativesImpl(env
);
115 AutofillPopupView
* AutofillPopupView::Create(
116 AutofillPopupController
* controller
) {
117 return new AutofillPopupViewAndroid(controller
);
120 } // namespace autofill