1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/android/autofill/autofill_dialog_view_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/android/window_android_helper.h"
#include "content/public/browser/web_contents.h"
#include "jni/AutofillDialogGlue_jni.h"
#include "ui/android/window_android.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/android/java_bitmap.h"
namespace autofill {
// AutofillDialogView ----------------------------------------------------------
// static
AutofillDialogView* AutofillDialogView::Create(
AutofillDialogController* controller) {
return new AutofillDialogViewAndroid(controller);
}
// AutofillDialogView ----------------------------------------------------------
AutofillDialogViewAndroid::AutofillDialogViewAndroid(
AutofillDialogController* controller)
: controller_(controller) {}
AutofillDialogViewAndroid::~AutofillDialogViewAndroid() {}
void AutofillDialogViewAndroid::Show() {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> use_billing_for_shipping_text =
base::android::ConvertUTF16ToJavaString(
env, controller_->UseBillingForShippingText());
ScopedJavaLocalRef<jstring> save_locally_text =
base::android::ConvertUTF16ToJavaString(
env, controller_->SaveLocallyText());
java_object_.Reset(Java_AutofillDialogGlue_create(
env,
reinterpret_cast<jint>(this),
WindowAndroidHelper::FromWebContents(controller_->web_contents())->
GetWindowAndroid()->GetJavaObject().obj(),
use_billing_for_shipping_text.obj(),
save_locally_text.obj()));
}
void AutofillDialogViewAndroid::Hide() {
NOTIMPLEMENTED();
}
void AutofillDialogViewAndroid::UpdateNotificationArea() {
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<DialogNotification> notifications =
controller_->CurrentNotifications();
const size_t count = notifications.size();
ScopedJavaLocalRef<jobjectArray> notification_array =
Java_AutofillDialogGlue_createAutofillDialogNotificationArray(
env, count);
for (size_t i = 0; i < count; ++i) {
ScopedJavaLocalRef<jstring> text =
base::android::ConvertUTF16ToJavaString(
env, notifications[i].display_text());
Java_AutofillDialogGlue_addToAutofillDialogNotificationArray(
env,
notification_array.obj(),
i,
static_cast<int>(notifications[i].GetBackgroundColor()),
static_cast<int>(notifications[i].GetTextColor()),
notifications[i].HasArrow(),
notifications[i].HasCheckbox(),
text.obj());
}
Java_AutofillDialogGlue_updateNotificationArea(env,
java_object_.obj(),
notification_array.obj());
}
void AutofillDialogViewAndroid::UpdateAccountChooser() {
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<string16> account_names;
int selected_account_index = -1;
ui::MenuModel* model = controller_->MenuModelForAccountChooser();
if (!model || controller_->ShouldShowSpinner()) {
account_names.push_back(controller_->AccountChooserText());
selected_account_index = 0;
} else {
for (int i = 0; i < model->GetItemCount(); ++i) {
if (model->IsItemCheckedAt(i))
selected_account_index = i;
account_names.push_back(model->GetLabelAt(i));
}
}
ScopedJavaLocalRef<jobjectArray> jaccount_names =
base::android::ToJavaArrayOfStrings(env, account_names);
Java_AutofillDialogGlue_updateAccountChooser(
env, java_object_.obj(), jaccount_names.obj(), selected_account_index);
}
void AutofillDialogViewAndroid::UpdateButtonStrip() {
NOTIMPLEMENTED();
}
void AutofillDialogViewAndroid::UpdateSection(DialogSection section,
UserInputAction action) {
// TODO(estade): respect |action|.
JNIEnv* env = base::android::AttachCurrentThread();
const DetailInputs& updated_inputs =
controller_->RequestedFieldsForSection(section);
const size_t inputCount = updated_inputs.size();
ScopedJavaLocalRef<jobjectArray> field_array =
Java_AutofillDialogGlue_createAutofillDialogFieldArray(
env, inputCount);
for (size_t i = 0; i < inputCount; ++i) {
const DetailInput& input = updated_inputs[i];
ScopedJavaLocalRef<jstring> autofilled =
base::android::ConvertUTF16ToJavaString(env, input.initial_value);
string16 placeholder16;
if (input.placeholder_text_rid > 0)
placeholder16 = l10n_util::GetStringUTF16(input.placeholder_text_rid);
ScopedJavaLocalRef<jstring> placeholder =
base::android::ConvertUTF16ToJavaString(env, placeholder16);
Java_AutofillDialogGlue_addToAutofillDialogFieldArray(
env,
field_array.obj(),
i,
reinterpret_cast<jint>(&input),
input.type,
placeholder.obj(),
autofilled.obj());
}
ui::MenuModel* menuModel = controller_->MenuModelForSection(section);
const int itemCount = menuModel->GetItemCount();
ScopedJavaLocalRef<jobjectArray> menu_array =
Java_AutofillDialogGlue_createAutofillDialogMenuItemArray(env,
itemCount);
int checkedItem = -1;
for (int i = 0; i < itemCount; ++i) {
if (menuModel->IsItemCheckedAt(i))
checkedItem = i;
ScopedJavaLocalRef<jstring> line1 =
base::android::ConvertUTF16ToJavaString(env, menuModel->GetLabelAt(i));
ScopedJavaLocalRef<jstring> line2 =
base::android::ConvertUTF16ToJavaString(env,
menuModel->GetSublabelAt(i));
ScopedJavaLocalRef<jobject> bitmap;
gfx::Image icon;
if (menuModel->GetIconAt(i, &icon)) {
const SkBitmap& sk_icon = icon.AsBitmap();
bitmap = gfx::ConvertToJavaBitmap(&sk_icon);
}
Java_AutofillDialogGlue_addToAutofillDialogMenuItemArray(
env, menu_array.obj(), i, line1.obj(), line2.obj(), bitmap.obj());
}
Java_AutofillDialogGlue_updateSection(env,
java_object_.obj(),
section,
controller_->SectionIsActive(section),
field_array.obj(),
menu_array.obj(),
checkedItem);
}
void AutofillDialogViewAndroid::GetUserInput(DialogSection section,
DetailOutputMap* output) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> fields =
Java_AutofillDialogGlue_getSection(env, java_object_.obj(), section);
DCHECK(fields.obj());
const int arrayLength = env->GetArrayLength(fields.obj());
for (int i = 0; i < arrayLength; ++i) {
ScopedJavaLocalRef<jobject> field(
env, env->GetObjectArrayElement(fields.obj(), i));
DetailInput* input = reinterpret_cast<DetailInput*>(
Java_AutofillDialogGlue_getFieldNativePointer(env, field.obj()));
string16 value = base::android::ConvertJavaStringToUTF16(
env, Java_AutofillDialogGlue_getFieldValue(env, field.obj()).obj());
output->insert(std::make_pair(input, value));
}
}
void AutofillDialogViewAndroid::UpdateSaveLocallyCheckBox() {
// TODO(aruslan) : Call this when at least one section is being edited.
JNIEnv* env = base::android::AttachCurrentThread();
Java_AutofillDialogGlue_updateSaveLocallyCheckBox(
env, java_object_.obj(), controller_->ShouldOfferToSaveInChrome());
}
string16 AutofillDialogViewAndroid::GetCvc() {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> cvc =
Java_AutofillDialogGlue_getCvc(env, java_object_.obj());
return base::android::ConvertJavaStringToUTF16(env, cvc.obj());
}
bool AutofillDialogViewAndroid::UseBillingForShipping() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_AutofillDialogGlue_shouldUseBillingForShipping(
env, java_object_.obj());
}
bool AutofillDialogViewAndroid::SaveDetailsLocally() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_AutofillDialogGlue_shouldSaveDetailsLocally(env,
java_object_.obj());
}
const content::NavigationController* AutofillDialogViewAndroid::ShowSignIn() {
NOTIMPLEMENTED();
return NULL;
}
void AutofillDialogViewAndroid::HideSignIn() {
NOTIMPLEMENTED();
}
// TODO(aruslan): bind to the automatic sign-in.
bool AutofillDialogViewAndroid::StartAutomaticSignIn(
const std::string& username) {
if (username.empty())
return false;
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> jusername =
base::android::ConvertUTF8ToJavaString(env, username);
Java_AutofillDialogGlue_startAutomaticSignIn(env, java_object_.obj(),
jusername.obj());
return true;
}
void AutofillDialogViewAndroid::UpdateProgressBar(double value) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_AutofillDialogGlue_updateProgressBar(env, java_object_.obj(), value);
}
void AutofillDialogViewAndroid::ModelChanged() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_AutofillDialogGlue_modelChanged(
env, java_object_.obj(),
controller_->ShouldShowSpinner());
UpdateSaveLocallyCheckBox();
UpdateSection(SECTION_EMAIL, CLEAR_USER_INPUT);
UpdateSection(SECTION_CC, CLEAR_USER_INPUT);
UpdateSection(SECTION_BILLING, CLEAR_USER_INPUT);
UpdateSection(SECTION_CC_BILLING, CLEAR_USER_INPUT);
UpdateSection(SECTION_SHIPPING, CLEAR_USER_INPUT);
}
void AutofillDialogViewAndroid::SubmitForTesting() {
controller_->OnAccept();
}
void AutofillDialogViewAndroid::CancelForTesting() {
controller_->OnCancel();
}
// TODO(aruslan): bind to the list of accounts population.
std::vector<std::string> AutofillDialogViewAndroid::GetAvailableUserAccounts() {
std::vector<std::string> account_names;
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> jaccount_names =
Java_AutofillDialogGlue_getUserAccountNames(env, java_object_.obj());
base::android::AppendJavaStringArrayToStringVector(
env, jaccount_names.obj(), &account_names);
return account_names;
}
// Calls from Java to C++
void AutofillDialogViewAndroid::ItemSelected(JNIEnv* env, jobject obj,
jint section, jint index) {
ui::MenuModel* menuModel =
controller_->MenuModelForSection(static_cast<DialogSection>(section));
menuModel->ActivatedAt(index);
}
ScopedJavaLocalRef<jobject> AutofillDialogViewAndroid::GetIconForField(
JNIEnv* env,
jobject obj,
jint field_id,
jstring jinput) {
string16 input = base::android::ConvertJavaStringToUTF16(env, jinput);
gfx::Image icon = controller_->
IconForField(static_cast<AutofillFieldType>(field_id), input);
const SkBitmap& sk_icon = icon.AsBitmap();
return gfx::ConvertToJavaBitmap(&sk_icon);
}
ScopedJavaLocalRef<jstring> AutofillDialogViewAndroid::GetPlaceholderForField(
JNIEnv* env,
jobject obj,
jint section,
jint field_id) {
// TODO(aruslan): We shouldn't be hardcoding this.
string16 cvc(ASCIIToUTF16("CVC"));
return base::android::ConvertUTF16ToJavaString(env, cvc);
}
void AutofillDialogViewAndroid::AccountSelected(JNIEnv* env, jobject obj,
jint index) {
ui::MenuModel* model = controller_->MenuModelForAccountChooser();
if (!model)
return;
model->ActivatedAt(index);
}
void AutofillDialogViewAndroid::EditingStart(JNIEnv* env, jobject obj,
jint jsection) {
const DialogSection section = static_cast<DialogSection>(jsection);
const SuggestionState& suggestion_state =
controller_->SuggestionStateForSection(section);
if (suggestion_state.text.empty()) {
// The section is already in the editing mode, or it's the "Add...".
return;
}
controller_->EditClickedForSection(section);
}
jboolean AutofillDialogViewAndroid::EditingComplete(JNIEnv* env,
jobject obj,
jint section) {
// Unfortunately, edits are not sent to the models, http://crbug.com/223919.
return ValidateSection(static_cast<DialogSection>(section),
AutofillDialogController::VALIDATE_FINAL);
}
void AutofillDialogViewAndroid::EditingCancel(JNIEnv* env,
jobject obj,
jint section) {
controller_->EditCancelledForSection(static_cast<DialogSection>(section));
}
ScopedJavaLocalRef<jstring> AutofillDialogViewAndroid::ValidateField(
JNIEnv* env,
jobject obj,
jint type,
jstring value) {
string16 field_value = base::android::ConvertJavaStringToUTF16(env, value);
AutofillFieldType field_type = static_cast<AutofillFieldType>(type);
if (controller_->InputIsValid(field_type, field_value)) {
return ScopedJavaLocalRef<jstring>();
} else {
// TODO(aurimas) Start using real error strings.
string16 error = ASCIIToUTF16("Error");
ScopedJavaLocalRef<jstring> error_text =
base::android::ConvertUTF16ToJavaString(env, error);
return error_text;
}
}
void AutofillDialogViewAndroid::ValidateSection(JNIEnv* env,
jobject obj,
jint section) {
ValidateSection(static_cast<DialogSection>(section),
AutofillDialogController::VALIDATE_EDIT);
}
void AutofillDialogViewAndroid::DialogSubmit(JNIEnv* env, jobject obj) {
// TODO(aurimas): add validation step.
controller_->OnAccept();
}
void AutofillDialogViewAndroid::DialogCancel(JNIEnv* env, jobject obj) {
controller_->OnCancel();
}
ScopedJavaLocalRef<jstring> AutofillDialogViewAndroid::GetLabelForSection(
JNIEnv* env,
jobject obj,
jint section) {
string16 label(controller_->LabelForSection(
static_cast<DialogSection>(section)));
return base::android::ConvertUTF16ToJavaString(env, label);
}
ScopedJavaLocalRef<jobjectArray> AutofillDialogViewAndroid::GetListForField(
JNIEnv* env,
jobject obj,
jint field) {
ui::ComboboxModel* model = controller_->ComboboxModelForAutofillType(
static_cast<AutofillFieldType>(field));
if (!model)
return ScopedJavaLocalRef<jobjectArray>();
std::vector<string16> list(model->GetItemCount());
for (int i = 0; i < model->GetItemCount(); ++i) {
list[i] = model->GetItemAt(i);
}
return base::android::ToJavaArrayOfStrings(env, list);
}
void AutofillDialogViewAndroid::ContinueAutomaticSignin(
JNIEnv* env, jobject obj,
jstring jaccount_name, jstring jsid, jstring jlsid) {
const std::string account_name =
base::android::ConvertJavaStringToUTF8(env, jaccount_name);
const std::string sid =
base::android::ConvertJavaStringToUTF8(env, jsid);
const std::string lsid =
base::android::ConvertJavaStringToUTF8(env, jlsid);
// TODO(aruslan): bind to the automatic sign-in.
// controller_->ContinueAutomaticSignIn(account_name, sid, lsid);
}
// static
bool AutofillDialogViewAndroid::RegisterAutofillDialogViewAndroid(JNIEnv* env) {
return RegisterNativesImpl(env);
}
bool AutofillDialogViewAndroid::ValidateSection(
DialogSection section, AutofillDialogController::ValidationType type) {
DetailOutputMap detail_outputs;
GetUserInput(section, &detail_outputs);
std::vector<AutofillFieldType> invalid_inputs = controller_->InputsAreValid(
detail_outputs, type);
const size_t item_count = invalid_inputs.size();
if (item_count == 0) return true;
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> error_array =
Java_AutofillDialogGlue_createAutofillDialogFieldError(env, item_count);
for (size_t i = 0; i < item_count; ++i) {
// TODO(aurimas) Start using real error strings.
string16 error = ASCIIToUTF16("Error");
ScopedJavaLocalRef<jstring> error_text =
base::android::ConvertUTF16ToJavaString(env, error);
Java_AutofillDialogGlue_addToAutofillDialogFieldErrorArray(
env, error_array.obj(), i, invalid_inputs[i], error_text.obj());
}
Java_AutofillDialogGlue_updateSectionErrors(env,
java_object_.obj(),
section,
error_array.obj());
return false;
}
} // namespace autofill
|