1 // Copyright (c) 2015 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_keyboard_accessory_view.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/popup_item_ids.h"
13 #include "components/autofill/core/browser/suggestion.h"
14 #include "grit/components_strings.h"
15 #include "jni/AutofillKeyboardAccessoryBridge_jni.h"
16 #include "ui/android/view_android.h"
17 #include "ui/android/window_android.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/geometry/rect.h"
26 void AddToJavaArray(const Suggestion
& suggestion
,
27 const AutofillPopupController
& controller
,
29 jobjectArray data_array
,
32 int android_icon_id
= 0;
33 if (!suggestion
.icon
.empty()) {
34 android_icon_id
= ResourceMapper::MapFromChromiumId(
35 controller
.GetIconResourceID(suggestion
.icon
));
38 Java_AutofillKeyboardAccessoryBridge_addToAutofillSuggestionArray(
39 env
, data_array
, position
,
40 base::android::ConvertUTF16ToJavaString(env
, suggestion
.value
).obj(),
41 base::android::ConvertUTF16ToJavaString(env
, suggestion
.label
).obj(),
42 android_icon_id
, suggestion
.frontend_id
, deletable
);
47 AutofillKeyboardAccessoryView::AutofillKeyboardAccessoryView(
48 AutofillPopupController
* controller
)
49 : controller_(controller
),
51 JNIEnv
* env
= base::android::AttachCurrentThread();
52 java_object_
.Reset(Java_AutofillKeyboardAccessoryBridge_create(env
));
55 AutofillKeyboardAccessoryView::~AutofillKeyboardAccessoryView() {
56 JNIEnv
* env
= base::android::AttachCurrentThread();
57 Java_AutofillKeyboardAccessoryBridge_resetNativeViewPointer(
58 env
, java_object_
.obj());
61 void AutofillKeyboardAccessoryView::Show() {
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 ui::ViewAndroid
* view_android
= controller_
->container_view();
65 Java_AutofillKeyboardAccessoryBridge_init(
66 env
, java_object_
.obj(),
67 reinterpret_cast<intptr_t>(this),
68 view_android
->GetWindowAndroid()->GetJavaObject().obj());
70 UpdateBoundsAndRedrawPopup();
73 void AutofillKeyboardAccessoryView::Hide() {
74 controller_
= nullptr;
75 JNIEnv
* env
= base::android::AttachCurrentThread();
76 Java_AutofillKeyboardAccessoryBridge_dismiss(env
, java_object_
.obj());
79 void AutofillKeyboardAccessoryView::UpdateBoundsAndRedrawPopup() {
80 JNIEnv
* env
= base::android::AttachCurrentThread();
81 size_t count
= controller_
->GetLineCount();
82 ScopedJavaLocalRef
<jobjectArray
> data_array
=
83 Java_AutofillKeyboardAccessoryBridge_createAutofillSuggestionArray(env
,
85 positions_
.resize(count
);
88 // Place "CLEAR FORM" item first in the list.
89 for (size_t i
= 0; i
< count
; ++i
) {
90 const Suggestion
& suggestion
= controller_
->GetSuggestionAt(i
);
91 if (suggestion
.frontend_id
== POPUP_ITEM_ID_CLEAR_FORM
) {
92 AddToJavaArray(suggestion
, *controller_
, env
, data_array
.obj(), position
,
94 positions_
[position
++] = i
;
98 for (size_t i
= 0; i
< count
; ++i
) {
99 const Suggestion
& suggestion
= controller_
->GetSuggestionAt(i
);
100 if (suggestion
.frontend_id
!= POPUP_ITEM_ID_CLEAR_FORM
) {
102 controller_
->GetRemovalConfirmationText(i
, nullptr, nullptr);
103 AddToJavaArray(suggestion
, *controller_
, env
, data_array
.obj(), position
,
105 positions_
[position
++] = i
;
109 Java_AutofillKeyboardAccessoryBridge_show(
110 env
, java_object_
.obj(), data_array
.obj(), controller_
->IsRTL());
113 void AutofillKeyboardAccessoryView::SuggestionSelected(JNIEnv
* env
,
116 // Race: Hide() may have already run.
118 controller_
->AcceptSuggestion(positions_
[list_index
]);
121 void AutofillKeyboardAccessoryView::DeletionRequested(JNIEnv
* env
,
127 base::string16 confirmation_title
, confirmation_body
;
128 if (!controller_
->GetRemovalConfirmationText(
129 positions_
[list_index
], &confirmation_title
, &confirmation_body
)) {
133 deleting_index_
= positions_
[list_index
];
134 Java_AutofillKeyboardAccessoryBridge_confirmDeletion(
135 env
, java_object_
.obj(),
136 base::android::ConvertUTF16ToJavaString(env
, confirmation_title
).obj(),
137 base::android::ConvertUTF16ToJavaString(env
, confirmation_body
).obj());
140 void AutofillKeyboardAccessoryView::DeletionConfirmed(JNIEnv
* env
,
145 CHECK_GE(deleting_index_
, 0);
146 controller_
->RemoveSuggestion(deleting_index_
);
149 void AutofillKeyboardAccessoryView::ViewDismissed(JNIEnv
* env
, jobject obj
) {
151 controller_
->ViewDestroyed();
156 void AutofillKeyboardAccessoryView::InvalidateRow(size_t) {
160 bool AutofillKeyboardAccessoryView::RegisterAutofillKeyboardAccessoryView(
162 return RegisterNativesImpl(env
);
165 } // namespace autofill