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..8b8bda81f96362de763f45cae5998ed2e612eddf |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/CountryAdapter.java |
@@ -0,0 +1,130 @@ |
+// 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 { |
+ LayoutInflater mInflater; |
aurimas (slooooooooow)
2014/08/01 15:18:21
Can this be private?
Evan Stade
2014/08/01 18:59:23
yes
|
+ |
+ @Override |
+ public int getCount() { |
+ return nativeGetItemCount(mCountryAdapterAndroid); |
+ } |
+ |
+ @Override |
+ public Object getItem(int position) { |
+ return nativeGetCountryCodeAt(mCountryAdapterAndroid, position); |
+ } |
+ |
+ @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(nativeGetCountryNameAt(mCountryAdapterAndroid, position)); |
+ 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(nativeGetCountryNameAt(mCountryAdapterAndroid, position)); |
+ 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/01 15:18:21
Doesn't this result in getCount() of JNI calls? JN
Evan Stade
2014/08/01 18:59:23
I was wondering about that. I figured JNI wasn't e
|
+ return i; |
aurimas (slooooooooow)
2014/08/01 15:18:21
move "return i;" to the same line with if statemen
Evan Stade
2014/08/01 18:59:23
Acknowledged.
|
+ } |
+ return 0; |
+ } |
+ |
+ private static CountryAdapter sAdapter; |
+ |
+ public static CountryAdapter getInstance() { |
aurimas (slooooooooow)
2014/08/01 15:18:21
Can you explain why are we using a static instance
Evan Stade
2014/08/01 18:59:23
What do you mean by the "real object"?
The reason
aurimas (slooooooooow)
2014/08/04 15:37:03
The way you are creating the CountryAdapter will m
Evan Stade
2014/08/04 20:36:04
That is how singletons tend to work. One benefit i
|
+ ThreadUtils.assertOnUiThread(); |
+ if (sAdapter == null) { |
+ sAdapter = new CountryAdapter(); |
+ } |
+ return sAdapter; |
+ } |
+ |
+ private final long mCountryAdapterAndroid; |
aurimas (slooooooooow)
2014/08/01 15:18:21
In java we put all member variable definitions to
Evan Stade
2014/08/01 18:59:22
OK. (I was basically copying PersonalDataManager.)
|
+ |
+ private CountryAdapter() { |
+ mCountryAdapterAndroid = nativeInit(); |
+ } |
+ |
+ private native long nativeInit(); |
+ private native int nativeGetItemCount(long nativeCountryAdapterAndroid); |
+ private native String nativeGetCountryCodeAt(long nativeCountryAdapterAndroid, int position); |
+ private native String nativeGetCountryNameAt(long nativeCountryAdapterAndroid, int position); |
+} |