Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / autofill / autofill_popup_view_android.cc
blobf26ee0f18ceb758570d7486f5ed7f9bbfccf7b7f
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 "base/command_line.h"
10 #include "chrome/browser/android/resource_mapper.h"
11 #include "chrome/browser/ui/android/autofill/autofill_keyboard_accessory_view.h"
12 #include "chrome/browser/ui/android/window_android_helper.h"
13 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
14 #include "components/autofill/core/browser/suggestion.h"
15 #include "components/autofill/core/common/autofill_util.h"
16 #include "content/public/browser/android/content_view_core.h"
17 #include "jni/AutofillPopupBridge_jni.h"
18 #include "ui/android/view_android.h"
19 #include "ui/android/window_android.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/android/java_bitmap.h"
22 #include "ui/gfx/geometry/rect_f.h"
24 namespace autofill {
26 AutofillPopupViewAndroid::AutofillPopupViewAndroid(
27 AutofillPopupController* controller)
28 : controller_(controller),
29 deleting_index_(-1) {}
31 AutofillPopupViewAndroid::~AutofillPopupViewAndroid() {}
33 void AutofillPopupViewAndroid::Show() {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 ui::ViewAndroid* view_android = controller_->container_view();
37 DCHECK(view_android);
39 java_object_.Reset(Java_AutofillPopupBridge_create(
40 env, reinterpret_cast<intptr_t>(this),
41 view_android->GetWindowAndroid()->GetJavaObject().obj(),
42 view_android->GetViewAndroidDelegate().obj()));
44 UpdateBoundsAndRedrawPopup();
47 void AutofillPopupViewAndroid::Hide() {
48 controller_ = NULL;
49 JNIEnv* env = base::android::AttachCurrentThread();
50 Java_AutofillPopupBridge_dismiss(env, java_object_.obj());
53 void AutofillPopupViewAndroid::UpdateBoundsAndRedrawPopup() {
54 JNIEnv* env = base::android::AttachCurrentThread();
55 Java_AutofillPopupBridge_setAnchorRect(
56 env,
57 java_object_.obj(),
58 controller_->element_bounds().x(),
59 controller_->element_bounds().y(),
60 controller_->element_bounds().width(),
61 controller_->element_bounds().height());
63 size_t count = controller_->GetLineCount();
64 ScopedJavaLocalRef<jobjectArray> data_array =
65 Java_AutofillPopupBridge_createAutofillSuggestionArray(env, count);
67 for (size_t i = 0; i < count; ++i) {
68 ScopedJavaLocalRef<jstring> value = base::android::ConvertUTF16ToJavaString(
69 env, controller_->GetElidedValueAt(i));
70 ScopedJavaLocalRef<jstring> label =
71 base::android::ConvertUTF16ToJavaString(
72 env, controller_->GetElidedLabelAt(i));
73 int android_icon_id = 0;
75 const autofill::Suggestion& suggestion = controller_->GetSuggestionAt(i);
76 if (!suggestion.icon.empty()) {
77 android_icon_id = ResourceMapper::MapFromChromiumId(
78 controller_->GetIconResourceID(suggestion.icon));
81 bool deletable =
82 controller_->GetRemovalConfirmationText(i, nullptr, nullptr);
83 Java_AutofillPopupBridge_addToAutofillSuggestionArray(
84 env,
85 data_array.obj(),
87 value.obj(),
88 label.obj(),
89 android_icon_id,
90 suggestion.frontend_id,
91 deletable);
94 Java_AutofillPopupBridge_show(
95 env, java_object_.obj(), data_array.obj(), controller_->IsRTL());
98 void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv* env,
99 jobject obj,
100 jint list_index) {
101 // Race: Hide() may have already run.
102 if (controller_)
103 controller_->AcceptSuggestion(list_index);
106 void AutofillPopupViewAndroid::DeletionRequested(JNIEnv* env,
107 jobject obj,
108 jint list_index) {
109 if (!controller_)
110 return;
112 base::string16 confirmation_title, confirmation_body;
113 if (!controller_->GetRemovalConfirmationText(list_index, &confirmation_title,
114 &confirmation_body)) {
115 return;
118 deleting_index_ = list_index;
119 Java_AutofillPopupBridge_confirmDeletion(
120 env,
121 java_object_.obj(),
122 base::android::ConvertUTF16ToJavaString(
123 env, confirmation_title).obj(),
124 base::android::ConvertUTF16ToJavaString(
125 env, confirmation_body).obj());
128 void AutofillPopupViewAndroid::DeletionConfirmed(JNIEnv* env,
129 jobject obj) {
130 if (!controller_)
131 return;
133 CHECK_GE(deleting_index_, 0);
134 controller_->RemoveSuggestion(deleting_index_);
137 void AutofillPopupViewAndroid::PopupDismissed(JNIEnv* env, jobject obj) {
138 if (controller_)
139 controller_->ViewDestroyed();
141 delete this;
144 void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
146 // static
147 bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv* env) {
148 return RegisterNativesImpl(env);
151 // static
152 AutofillPopupView* AutofillPopupView::Create(
153 AutofillPopupController* controller) {
154 if (IsKeyboardAccessoryEnabled())
155 return new AutofillKeyboardAccessoryView(controller);
157 return new AutofillPopupViewAndroid(controller);
160 } // namespace autofill