Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / autofill / CountryAdapter.java
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 package org.chromium.chrome.browser.autofill;
6
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;
13
14 import org.chromium.base.JNINamespace;
15 import org.chromium.chrome.R;
16
17 /**
18  * Android wrapper for CountryComboboxModel.
19  *
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.
22  */
23 @JNINamespace("autofill")
24 public class CountryAdapter implements SpinnerAdapter {
25     /**
26      * The items to show in the spinner.
27      *
28      * Even indices are display names, odd indices are country codes.
29      */
30     private String[] mItems;
31
32     private LayoutInflater mInflater;
33     private final long mCountryAdapterAndroid;
34
35     public CountryAdapter(LayoutInflater inflater) {
36         mInflater = inflater;
37         mCountryAdapterAndroid = nativeInit();
38         mItems = nativeGetItems(mCountryAdapterAndroid);
39     }
40
41     @Override
42     public int getCount() {
43         return mItems.length / 2;
44     }
45
46     @Override
47     public Object getItem(int position) {
48         return mItems[position * 2 + 1];
49     }
50
51     @Override
52     public long getItemId(int position) {
53         return position;
54     }
55
56     @Override
57     public int getItemViewType(int position) {
58         return 0;
59     }
60
61     @Override
62     public View getView(int position, View convertView, ViewGroup parent) {
63         TextView textView = null;
64         if (convertView instanceof TextView) {
65             textView = (TextView) convertView;
66         }
67         if (textView == null) {
68             textView = (TextView) mInflater.inflate(R.layout.country_text, parent, false);
69         }
70
71         textView.setText(mItems[position * 2]);
72         return textView;
73     }
74
75     @Override
76     public boolean hasStableIds() {
77         return true;
78     }
79
80     @Override
81     public int getViewTypeCount() {
82         return 1;
83     }
84
85     @Override
86     public boolean isEmpty() {
87         return false;
88     }
89
90     @Override
91     public void registerDataSetObserver(DataSetObserver observer) {}
92
93     @Override
94     public void unregisterDataSetObserver(DataSetObserver observer) {}
95
96     @Override
97     public View getDropDownView(int position, View convertView, ViewGroup parent) {
98         TextView textView = null;
99         if (convertView instanceof TextView) {
100             textView = (TextView) convertView;
101         }
102         if (textView == null) {
103             textView = (TextView) mInflater.inflate(R.layout.country_item, parent, false);
104         }
105
106         textView.setText(mItems[position * 2]);
107         return textView;
108     }
109
110     /**
111      * Gets the index in the model for the given country code.
112      */
113     public int getIndexForCountryCode(String countryCode) {
114         for (int i = 0; i < getCount(); i++) {
115             if (countryCode.equals(getItem(i))) {
116                 return i;
117             }
118         }
119         return 0;
120     }
121
122     private native long nativeInit();
123     private native String[] nativeGetItems(long nativeCountryAdapterAndroid);
124 }