Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / android / autofill / autofill_popup_view_android.cc
blob8297968367ba132fdac33d98e38ccd964deab6a8
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"
21 namespace autofill {
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();
33 DCHECK(view_android);
35 java_object_.Reset(Java_AutofillPopupBridge_create(
36 env,
37 reinterpret_cast<intptr_t>(this),
38 view_android->GetWindowAndroid()->GetJavaObject().obj(),
39 view_android->GetJavaObject().obj()));
41 UpdateBoundsAndRedrawPopup();
44 void AutofillPopupViewAndroid::Hide() {
45 controller_ = NULL;
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(
53 env,
54 java_object_.obj(),
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(
79 env,
80 data_array.obj(),
82 value.obj(),
83 label.obj(),
84 android_icon_id,
85 suggestion.frontend_id);
88 Java_AutofillPopupBridge_show(
89 env, java_object_.obj(), data_array.obj(), controller_->IsRTL());
92 void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv* env,
93 jobject obj,
94 jint list_index) {
95 // Race: Hide() may have already run.
96 if (controller_)
97 controller_->AcceptSuggestion(list_index);
100 void AutofillPopupViewAndroid::PopupDismissed(JNIEnv* env, jobject obj) {
101 if (controller_)
102 controller_->ViewDestroyed();
104 delete this;
107 void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
109 // static
110 bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv* env) {
111 return RegisterNativesImpl(env);
114 // static
115 AutofillPopupView* AutofillPopupView::Create(
116 AutofillPopupController* controller) {
117 return new AutofillPopupViewAndroid(controller);
120 } // namespace autofill