OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/android/preferences/autofill/autofill_profile_bridge.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_array.h" | |
11 #include "base/android/jni_string.h" | |
12 #include "base/bind.h" | |
13 #include "jni/AutofillProfileBridge_jni.h" | |
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h" | |
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui _component.h" | |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati on.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 | |
19 using base::android::ConvertJavaStringToUTF8; | |
20 using base::android::ToJavaArrayOfStrings; | |
21 using ::i18n::addressinput::AddressUiComponent; | |
22 using ::i18n::addressinput::BuildComponents; | |
23 using ::i18n::addressinput::GetRegionCodes; | |
newt (away)
2015/01/27 01:45:07
If you're only calling a method once, I'd skip usi
Theresa
2015/01/28 02:05:42
Done.
| |
24 using ::i18n::addressinput::Localization; | |
25 | |
26 namespace autofill{ | |
27 namespace android{ | |
28 | |
29 static void GetAvailableCountries(JNIEnv* env, | |
30 jclass clazz, | |
31 jobject j_country_list) { | |
32 std::vector<std::string> regionCodes = GetRegionCodes(); | |
33 Java_AutofillProfileBridge_stringArrayToList(env, | |
34 ToJavaArrayOfStrings( | |
35 env, regionCodes).obj(), | |
36 j_country_list); | |
37 } | |
38 | |
39 static void GetAddressUiComponents(JNIEnv* env, | |
40 jclass clazz, | |
41 jstring country_code, | |
42 jstring ui_language_tag, | |
newt (away)
2015/01/27 01:45:06
I'd call this j_language_tag (and j_country_code)
Theresa
2015/01/28 02:05:42
Done.
| |
43 jobject j_id_list, | |
44 jobject j_name_list) { | |
45 std::string best_language_tag; | |
newt (away)
2015/01/27 01:45:06
We never use this value. Should we? / why does it
Theresa
2015/01/28 02:05:42
It's passed in to the ::i18n::addressinput::BuildC
| |
46 std::string language_code; | |
47 std::vector<std::string> componentIds; | |
newt (away)
2015/01/27 01:45:07
Use hacker_case not camelCase (it's easy to forge
Theresa
2015/01/28 02:05:42
Done.
| |
48 std::vector<std::string> componentLabels; | |
49 Localization localization; | |
50 localization.SetGetter(l10n_util::GetStringUTF8); | |
51 | |
52 // Format ui_langage_tag by replacing _'s with -'s. The Java strings coming | |
53 // from Android are in the format en_US and the C++ equivalent is en-US | |
54 std::string language_tag = ConvertJavaStringToUTF8(env, ui_language_tag); | |
newt (away)
2015/01/27 01:45:06
language_tag is never used. Did you mean to use it
Theresa
2015/01/28 02:05:42
Yep. Done.
| |
55 std::replace(language_tag.begin(), language_tag.end(), '_', '-'); | |
56 | |
57 std::vector<AddressUiComponent> uiComponents = BuildComponents( | |
58 ConvertJavaStringToUTF8(env, country_code), localization, | |
59 ConvertJavaStringToUTF8(env, ui_language_tag), &best_language_tag); | |
60 | |
61 for (auto uiComponent : uiComponents) { | |
62 // TODO(twellington): debug localization problem: | |
newt (away)
2015/01/27 01:45:06
You can remove this comment. I filed a bug to trac
Theresa
2015/01/28 02:05:42
Done.
| |
63 // If the language is changed in the phone's settings menu after Chrome | |
64 // settings have been launched, when this method gets triggered the strings | |
65 // returned are still in the original language. E.g. If I open Chrome | |
66 // settings then switch from English to Spanish, then go to the autofill | |
67 // address form, the field labels (obtained from this function) are | |
68 // still in English. If I back out of settings then reopen settings and go | |
69 // to the autofill address form, the field labels are in Spanish. | |
70 componentLabels.push_back(uiComponent.name); | |
71 | |
72 switch (uiComponent.field) { | |
73 case ::i18n::addressinput::COUNTRY: | |
74 componentIds.push_back("country"); | |
newt (away)
2015/01/27 01:45:07
Is there not a string constant we could use here i
Theresa
2015/01/28 02:05:42
Done.
| |
75 break; | |
76 case ::i18n::addressinput::ADMIN_AREA: | |
77 componentIds.push_back("admin_area"); | |
78 break; | |
79 case ::i18n::addressinput::LOCALITY: | |
80 componentIds.push_back("locality"); | |
81 break; | |
82 case ::i18n::addressinput::DEPENDENT_LOCALITY: | |
83 componentIds.push_back("dependent_locality"); | |
84 break; | |
85 case ::i18n::addressinput::SORTING_CODE: | |
86 componentIds.push_back("sorting_code"); | |
87 break; | |
88 case ::i18n::addressinput::POSTAL_CODE: | |
89 componentIds.push_back("postal_code"); | |
90 break; | |
91 case ::i18n::addressinput::STREET_ADDRESS: | |
92 componentIds.push_back("street_address"); | |
93 break; | |
94 case ::i18n::addressinput::ORGANIZATION: | |
95 componentIds.push_back("organization"); | |
96 break; | |
97 case ::i18n::addressinput::RECIPIENT: | |
98 componentIds.push_back("recipient"); | |
99 break; | |
100 } | |
newt (away)
2015/01/27 01:45:06
Adding a default case seems like a good idea:
d
Theresa
2015/01/28 02:05:42
Done.
| |
101 } | |
102 | |
103 Java_AutofillProfileBridge_stringArrayToList(env, | |
104 ToJavaArrayOfStrings( | |
105 env, componentIds).obj(), | |
106 j_id_list); | |
107 Java_AutofillProfileBridge_stringArrayToList(env, | |
108 ToJavaArrayOfStrings( | |
109 env, componentLabels).obj(), | |
110 j_name_list); | |
111 } | |
112 | |
113 // static | |
114 bool RegisterAutofillProfileBridge(JNIEnv* env) { | |
115 return RegisterNativesImpl(env); | |
116 } | |
117 | |
118 } // namespace android | |
119 } // namespace autofill | |
OLD | NEW |