OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/android/autofill/autofill_keyboard_accessory_view.h" |
| 6 |
| 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 "jni/AutofillKeyboardAccessoryBridge_jni.h" |
| 14 #include "ui/android/view_android.h" |
| 15 #include "ui/android/window_android.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/geometry/rect.h" |
| 18 |
| 19 namespace autofill { |
| 20 |
| 21 AutofillKeyboardAccessoryView::AutofillKeyboardAccessoryView( |
| 22 AutofillPopupController* controller) |
| 23 : controller_(controller) { |
| 24 JNIEnv* env = base::android::AttachCurrentThread(); |
| 25 java_object_.Reset(Java_AutofillKeyboardAccessoryBridge_create(env)); |
| 26 } |
| 27 |
| 28 AutofillKeyboardAccessoryView::~AutofillKeyboardAccessoryView() { |
| 29 JNIEnv* env = base::android::AttachCurrentThread(); |
| 30 Java_AutofillKeyboardAccessoryBridge_nativeViewDestroyed( |
| 31 env, java_object_.obj()); |
| 32 } |
| 33 |
| 34 void AutofillKeyboardAccessoryView::Show() { |
| 35 JNIEnv* env = base::android::AttachCurrentThread(); |
| 36 ui::ViewAndroid* view_android = controller_->container_view(); |
| 37 DCHECK(view_android); |
| 38 Java_AutofillKeyboardAccessoryBridge_init( |
| 39 env, java_object_.obj(), |
| 40 reinterpret_cast<intptr_t>(this), |
| 41 view_android->GetWindowAndroid()->GetJavaObject().obj()); |
| 42 |
| 43 UpdateBoundsAndRedrawPopup(); |
| 44 } |
| 45 |
| 46 void AutofillKeyboardAccessoryView::Hide() { |
| 47 controller_ = nullptr; |
| 48 JNIEnv* env = base::android::AttachCurrentThread(); |
| 49 Java_AutofillKeyboardAccessoryBridge_dismiss(env, java_object_.obj()); |
| 50 } |
| 51 |
| 52 void AutofillKeyboardAccessoryView::UpdateBoundsAndRedrawPopup() { |
| 53 JNIEnv* env = base::android::AttachCurrentThread(); |
| 54 size_t count = controller_->GetLineCount(); |
| 55 ScopedJavaLocalRef<jobjectArray> data_array = |
| 56 Java_AutofillKeyboardAccessoryBridge_createAutofillSuggestionArray(env, |
| 57 count); |
| 58 |
| 59 for (size_t i = 0; i < count; ++i) { |
| 60 ScopedJavaLocalRef<jstring> value = base::android::ConvertUTF16ToJavaString( |
| 61 env, controller_->GetElidedValueAt(i)); |
| 62 ScopedJavaLocalRef<jstring> label = base::android::ConvertUTF16ToJavaString( |
| 63 env, controller_->GetElidedLabelAt(i)); |
| 64 int android_icon_id = 0; |
| 65 |
| 66 const autofill::Suggestion& suggestion = controller_->GetSuggestionAt(i); |
| 67 if (!suggestion.icon.empty()) { |
| 68 android_icon_id = ResourceMapper::MapFromChromiumId( |
| 69 controller_->GetIconResourceID(suggestion.icon)); |
| 70 } |
| 71 |
| 72 Java_AutofillKeyboardAccessoryBridge_addToAutofillSuggestionArray( |
| 73 env, data_array.obj(), i, value.obj(), label.obj(), android_icon_id, |
| 74 suggestion.frontend_id); |
| 75 } |
| 76 |
| 77 Java_AutofillKeyboardAccessoryBridge_show( |
| 78 env, java_object_.obj(), data_array.obj(), controller_->IsRTL()); |
| 79 } |
| 80 |
| 81 void AutofillKeyboardAccessoryView::SuggestionSelected(JNIEnv* env, |
| 82 jobject obj, |
| 83 jint list_index) { |
| 84 // Race: Hide() may have already run. |
| 85 if (controller_) |
| 86 controller_->AcceptSuggestion(list_index); |
| 87 } |
| 88 |
| 89 void AutofillKeyboardAccessoryView::ViewDismissed(JNIEnv* env, jobject obj) { |
| 90 if (controller_) |
| 91 controller_->ViewDestroyed(); |
| 92 |
| 93 delete this; |
| 94 } |
| 95 |
| 96 void AutofillKeyboardAccessoryView::InvalidateRow(size_t) { |
| 97 } |
| 98 |
| 99 // static |
| 100 bool AutofillKeyboardAccessoryView::RegisterAutofillKeyboardAccessoryView( |
| 101 JNIEnv* env) { |
| 102 return RegisterNativesImpl(env); |
| 103 } |
| 104 |
| 105 } // namespace autofill |
OLD | NEW |