Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/autofill/CountryAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/CountryAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/CountryAdapter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c8d3870d57a6cb6480661b328109b03cc2bdab9 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/CountryAdapter.java |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2014 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. |
| + |
| +package org.chromium.chrome.browser.autofill; |
| + |
| +import android.database.DataSetObserver; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.SpinnerAdapter; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.chrome.R; |
| + |
| +/** |
| + * Android wrapper for CountryComboboxModel. |
| + * |
| + * Only useable from the UI layer. Used in the Android settings UI. |
| + * |
| + * See chrome/browser/ui/android/autofill/country_adapter_android.h for more details. |
| + */ |
| +@JNINamespace("autofill") |
| +public class CountryAdapter implements SpinnerAdapter { |
| + /** |
| + * The items to show in the spinner. |
| + * |
| + * Even indices are display names, odd indices are country codes. |
| + */ |
| + private String[] mItems; |
| + |
| + private LayoutInflater mInflater; |
| + private static CountryAdapter sAdapter; |
| + private final long mCountryAdapterAndroid; |
| + |
| + @Override |
| + public int getCount() { |
| + return mItems.length / 2; |
| + } |
| + |
| + @Override |
| + public Object getItem(int position) { |
| + return mItems[position * 2 + 1]; |
| + } |
| + |
| + @Override |
| + public long getItemId(int position) { |
| + return position; |
| + } |
| + |
| + @Override |
| + public int getItemViewType(int position) { |
| + return 0; |
| + } |
| + |
| + @Override |
| + public View getView(int position, View convertView, ViewGroup parent) { |
| + TextView textView = (TextView) convertView; |
| + if (textView == null) { |
| + textView = (TextView) mInflater.inflate(R.layout.country_text, parent, false); |
| + } |
| + |
| + textView.setText(mItems[position * 2]); |
| + return textView; |
| + } |
| + |
| + @Override |
| + public boolean hasStableIds() { |
| + return true; |
| + } |
| + |
| + @Override |
| + public int getViewTypeCount() { |
| + return 1; |
| + } |
| + |
| + @Override |
| + public boolean isEmpty() { |
| + return false; |
| + } |
| + |
| + @Override |
| + public void registerDataSetObserver(DataSetObserver observer) {} |
| + |
| + @Override |
| + public void unregisterDataSetObserver(DataSetObserver observer) {} |
| + |
| + @Override |
| + public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| + TextView textView = (TextView) convertView; |
| + if (textView == null) { |
| + textView = (TextView) mInflater.inflate(R.layout.country_item, parent, false); |
| + } |
| + |
| + textView.setText(mItems[position * 2]); |
| + return textView; |
| + } |
| + |
| + /** |
| + * Sets the LayoutInflater used for making views. |
| + */ |
| + public void setLayoutInflater(LayoutInflater inflater) { |
| + mInflater = inflater; |
| + } |
| + |
| + /** |
| + * Gets the index in the model for the given country code. |
| + */ |
| + public int getIndexForCountryCode(String countryCode) { |
| + for (int i = 0; i < getCount(); i++) { |
| + if (countryCode.equals(getItem(i))) |
|
aurimas (slooooooooow)
2014/08/06 22:41:23
Move return i; to the same line with the if statem
Evan Stade
2014/08/09 00:22:16
what rule governs the placement of the return? In
|
| + return i; |
| + } |
| + return 0; |
| + } |
| + |
| + public static CountryAdapter getInstance() { |
| + ThreadUtils.assertOnUiThread(); |
| + if (sAdapter == null) { |
|
aurimas (slooooooooow)
2014/08/06 22:41:23
Chrome supports multi-instance mode (for samsung d
Evan Stade
2014/08/09 00:22:16
Done.
|
| + sAdapter = new CountryAdapter(); |
|
aurimas (slooooooooow)
2014/08/06 22:41:23
Indenting is wrong +2 spaces.
Evan Stade
2014/08/09 00:22:16
fn removed
|
| + } |
| + return sAdapter; |
| + } |
| + |
| + private CountryAdapter() { |
| + mCountryAdapterAndroid = nativeInit(); |
| + mItems = nativeGetItems(mCountryAdapterAndroid); |
| + } |
| + |
| + private native long nativeInit(); |
| + private native String[] nativeGetItems(long nativeCountryAdapterAndroid); |
| +} |