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.
5 package org.chromium.chrome.browser.autofill;
7 import android.database.DataSetObserver;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.SpinnerAdapter;
12 import android.widget.TextView;
14 import org.chromium.base.JNINamespace;
15 import org.chromium.chrome.R;
18 * Android wrapper for CountryComboboxModel.
20 * Only useable from the UI layer. Used in the Android settings UI.
21 * See chrome/browser/ui/android/autofill/country_adapter_android.h for more details.
23 @JNINamespace("autofill")
24 public class CountryAdapter implements SpinnerAdapter {
26 * The items to show in the spinner.
28 * Even indices are display names, odd indices are country codes.
30 private String[] mItems;
32 private LayoutInflater mInflater;
33 private final long mCountryAdapterAndroid;
35 public CountryAdapter(LayoutInflater inflater) {
37 mCountryAdapterAndroid = nativeInit();
38 mItems = nativeGetItems(mCountryAdapterAndroid);
42 public int getCount() {
43 return mItems.length / 2;
47 public Object getItem(int position) {
48 return mItems[position * 2 + 1];
52 public long getItemId(int position) {
57 public int getItemViewType(int position) {
62 public View getView(int position, View convertView, ViewGroup parent) {
63 TextView textView = null;
64 if (convertView instanceof TextView) {
65 textView = (TextView) convertView;
67 if (textView == null) {
68 textView = (TextView) mInflater.inflate(R.layout.country_text, parent, false);
71 textView.setText(mItems[position * 2]);
76 public boolean hasStableIds() {
81 public int getViewTypeCount() {
86 public boolean isEmpty() {
91 public void registerDataSetObserver(DataSetObserver observer) {}
94 public void unregisterDataSetObserver(DataSetObserver observer) {}
97 public View getDropDownView(int position, View convertView, ViewGroup parent) {
98 TextView textView = null;
99 if (convertView instanceof TextView) {
100 textView = (TextView) convertView;
102 if (textView == null) {
103 textView = (TextView) mInflater.inflate(R.layout.country_item, parent, false);
106 textView.setText(mItems[position * 2]);
111 * Gets the index in the model for the given country code.
113 public int getIndexForCountryCode(String countryCode) {
114 for (int i = 0; i < getCount(); i++) {
115 if (countryCode.equals(getItem(i))) {
122 private native long nativeInit();
123 private native String[] nativeGetItems(long nativeCountryAdapterAndroid);