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.
4
5#include "chrome/browser/ui/android/autofill/autofill_popup_view_android.h"
6
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"
16
17namespace autofill {
18
19AutofillPopupViewAndroid::AutofillPopupViewAndroid(
20    AutofillPopupController* controller)
21    : controller_(controller) {}
22
23AutofillPopupViewAndroid::~AutofillPopupViewAndroid() {}
24
25void AutofillPopupViewAndroid::Show() {
26  JNIEnv* env = base::android::AttachCurrentThread();
27  ui::ViewAndroid* view_android = controller_->container_view();
28
29  DCHECK(view_android);
30
31  java_object_.Reset(Java_AutofillPopupBridge_create(
32      env,
33      reinterpret_cast<intptr_t>(this),
34      view_android->GetWindowAndroid()->GetJavaObject().obj(),
35      view_android->GetJavaObject().obj()));
36
37  UpdateBoundsAndRedrawPopup();
38}
39
40void AutofillPopupViewAndroid::Hide() {
41  JNIEnv* env = base::android::AttachCurrentThread();
42  Java_AutofillPopupBridge_hide(env, java_object_.obj());
43  delete this;
44}
45
46void AutofillPopupViewAndroid::UpdateBoundsAndRedrawPopup() {
47  JNIEnv* env = base::android::AttachCurrentThread();
48  Java_AutofillPopupBridge_setAnchorRect(env,
49                                       java_object_.obj(),
50                                       controller_->element_bounds().x(),
51                                       controller_->element_bounds().y(),
52                                       controller_->element_bounds().width(),
53                                       controller_->element_bounds().height());
54
55  // We need an array of AutofillSuggestion.
56  size_t count = controller_->names().size();
57
58  ScopedJavaLocalRef<jobjectArray> data_array =
59      Java_AutofillPopupBridge_createAutofillSuggestionArray(env, count);
60
61  for (size_t i = 0; i < count; ++i) {
62    ScopedJavaLocalRef<jstring> name =
63        base::android::ConvertUTF16ToJavaString(env, controller_->names()[i]);
64    ScopedJavaLocalRef<jstring> subtext =
65        base::android::ConvertUTF16ToJavaString(env,
66                                                controller_->subtexts()[i]);
67    Java_AutofillPopupBridge_addToAutofillSuggestionArray(
68        env,
69        data_array.obj(),
70        i,
71        name.obj(),
72        subtext.obj(),
73        controller_->identifiers()[i]);
74  }
75
76  Java_AutofillPopupBridge_show(env, java_object_.obj(), data_array.obj());
77}
78
79void AutofillPopupViewAndroid::SuggestionSelected(JNIEnv* env,
80                                                  jobject obj,
81                                                  jint list_index) {
82  controller_->AcceptSuggestion(list_index);
83}
84
85void AutofillPopupViewAndroid::RequestHide(JNIEnv* env, jobject obj) {
86  controller_->Hide();
87}
88
89void AutofillPopupViewAndroid::InvalidateRow(size_t) {}
90
91// static
92bool AutofillPopupViewAndroid::RegisterAutofillPopupViewAndroid(JNIEnv* env) {
93  return RegisterNativesImpl(env);
94}
95
96// static
97AutofillPopupView* AutofillPopupView::Create(
98    AutofillPopupController* controller) {
99  return new AutofillPopupViewAndroid(controller);
100}
101
102}  // namespace autofill
103