Index: chrome/android/java/src/org/chromium/chrome/browser/autofill/PasswordGenerationAdapter.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/PasswordGenerationAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PasswordGenerationAdapter.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a27bb92bca2b13e5e058deb779dafdc2417d6bc1 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PasswordGenerationAdapter.java |
@@ -0,0 +1,196 @@ |
+// 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.content.Context; |
+import android.text.SpannableString; |
+import android.text.Spanned; |
+import android.text.TextPaint; |
+import android.text.method.LinkMovementMethod; |
+import android.text.style.ClickableSpan; |
+import android.view.LayoutInflater; |
+import android.view.View; |
+import android.view.ViewGroup; |
+import android.widget.BaseAdapter; |
+import android.widget.TextView; |
+ |
+import org.chromium.chrome.R; |
+ |
+import java.util.Arrays; |
+import java.util.List; |
+ |
+/** |
+ * The adapter that populates the list popup for password generation with data. |
+ */ |
+public class PasswordGenerationAdapter extends BaseAdapter { |
+ private final Context mContext; |
+ private final List<Integer> mViews; |
+ private final int mMinimumWidth; |
+ private final String mPassword; |
+ private final String mSuggestionTitle; |
+ private final SpannableString mExplanationSpan; |
+ |
+ /** |
+ * UI shows a generated password suggestion. |
+ */ |
+ private static final int SUGGESTION = 0; |
+ |
+ /** |
+ * UI shows an explanation about storing passwords in Chrome. |
+ */ |
+ private static final int EXPLANATION = 1; |
+ |
+ /** |
+ * Handler for clicks on the "saved passwords" link. |
+ */ |
+ public interface Delegate { |
+ /** |
+ * Called when the user clicks the "saved passwords" link. |
+ */ |
+ public void onSavedPasswordsLinkClicked(); |
+ } |
+ |
+ /** |
+ * Builds the adapter to display views using data from delegate. |
+ * @param context Android context. |
+ * @param delegate The handler for clicking on the "saved passwords" link. |
+ * @param minimumWidth The minimum width for the popup. |
+ * @param passwordDisplayed Whether the auto-generated password should be suggested. |
+ * @param password The auto-generated password to suggest. |
+ * @param suggestionTitle The translated title of the suggestion part of the UI. |
+ * @param explanationText The translated text for the explanation part of the UI. |
+ * @param explanationTextLinkRangeStart The start of the range in the explanation text that |
+ * should be a link to the saved passwords. |
+ * @param explanationTextLinkRangeEnd The end of the range in the explanation text that should |
+ * be a link to the saved passwords. |
+ */ |
+ public PasswordGenerationAdapter(Context context, final Delegate delegate, int minimumWidth, |
+ boolean passwordDisplayed, String password, String suggestionTitle, |
+ String explanationText, int explanationTextLinkRangeStart, |
+ int explanationTextLinkRangeEnd) { |
+ super(); |
+ mContext = context; |
+ mViews = passwordDisplayed ? Arrays.asList(SUGGESTION, EXPLANATION) : Arrays |
+ .asList(EXPLANATION); |
+ mMinimumWidth = minimumWidth; |
+ mPassword = password; |
+ mSuggestionTitle = suggestionTitle; |
+ |
+ mExplanationSpan = new SpannableString(explanationText); |
+ mExplanationSpan.setSpan( |
+ new ClickableSpan() { |
+ @Override |
+ public void onClick(View view) { |
+ delegate.onSavedPasswordsLinkClicked(); |
+ } |
+ |
+ @Override |
+ public void updateDrawState(TextPaint textPaint) { |
+ textPaint.setUnderlineText(false); |
+ textPaint.setColor(mContext.getResources().getColor( |
+ R.color.password_generation_link_text_color)); |
+ } |
+ }, explanationTextLinkRangeStart, explanationTextLinkRangeEnd, |
+ Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
+ } |
+ |
+ /** |
+ * Used by list popup window to draw an element. |
+ * @param position The position of the element in the popup list. |
+ * @param convertView If not null, the element view where the data goes. |
+ * @param parent The list popup. |
+ * @return The view of the popup list element at the given position. |
+ */ |
+ @Override |
+ public View getView(int position, View convertView, ViewGroup parent) { |
+ LayoutInflater inflater = (LayoutInflater) mContext |
+ .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
+ int item = getItemViewType(position); |
+ switch (item) { |
+ case SUGGESTION: |
+ if (convertView == null) { |
+ convertView = inflater.inflate(R.layout.password_generation_popup_suggestion, |
+ null); |
+ } |
+ |
+ View suggestion = convertView.findViewById(R.id.password_generation_suggestion); |
+ if (suggestion == null) { |
+ convertView = inflater.inflate(R.layout.password_generation_popup_suggestion, |
+ null); |
+ suggestion = convertView.findViewById(R.id.password_generation_suggestion); |
+ } |
+ TextView title = (TextView) convertView |
+ .findViewById(R.id.password_generation_title); |
+ TextView password = (TextView) convertView |
+ .findViewById(R.id.password_generation_password); |
+ suggestion.setMinimumWidth(mMinimumWidth); |
+ title.setText(mSuggestionTitle); |
+ password.setText(mPassword); |
+ break; |
+ case EXPLANATION: |
+ if (convertView == null) { |
+ convertView = inflater.inflate(R.layout.password_generation_popup_explanation, |
+ null); |
+ } |
+ TextView explanation = (TextView) convertView |
+ .findViewById(R.id.password_generation_explanation); |
+ if (explanation == null) { |
+ convertView = inflater.inflate(R.layout.password_generation_popup_explanation, |
+ null); |
+ explanation = (TextView) convertView |
+ .findViewById(R.id.password_generation_explanation); |
please use gerrit instead
2014/10/22 18:40:51
Sometimes convertView for EXPLANATION has the layo
please use gerrit instead
2014/10/22 21:04:36
Figured it out: it's our own DropdownPopupWindow.m
|
+ } |
+ |
+ explanation.setText(mExplanationSpan); |
+ explanation.setMovementMethod(LinkMovementMethod.getInstance()); |
+ break; |
+ default: |
+ assert false : "Unknown item"; |
+ } |
+ |
+ return convertView; |
+ } |
+ |
+ @Override |
+ public Object getItem(int position) { |
+ return null; |
+ } |
+ |
+ @Override |
+ public long getItemId(int position) { |
+ return mViews.get(position); |
+ } |
+ |
+ @Override |
+ public int getItemViewType(int position) { |
+ return mViews.get(position); |
+ } |
+ |
+ @Override |
+ public int getCount() { |
+ return mViews.size(); |
+ } |
+ |
+ /** |
+ * Used by list popup window to check if all of the elements are enabled. All password |
+ * generation popups have an explanation element, which is not selectable. Therefore, this |
+ * method always returns false: some of the items are disabled. |
+ * @return boolean Always false. |
+ */ |
+ @Override |
+ public boolean areAllItemsEnabled() { |
+ return false; |
+ } |
+ |
+ /** |
+ * Used by list popup window to check if the element at this position is enabled. Only the |
+ * suggestion element is enabled. |
+ * @return boolean True if the view at position is a suggestion. |
+ */ |
+ @Override |
+ public boolean isEnabled(int position) { |
+ return getItemViewType(position) == SUGGESTION; |
+ } |
+} |