Chromium Code Reviews| 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..53153aacfa3eb135e23097e8aa22aac416510768 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PasswordGenerationAdapter.java |
| @@ -0,0 +1,214 @@ |
| +// 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.View.MeasureSpec; |
| +import android.view.ViewGroup; |
| +import android.widget.BaseAdapter; |
| +import android.widget.RelativeLayout.LayoutParams; |
| +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. |
|
aurimas (slooooooooow)
2014/10/23 00:57:01
Can you update the comment to explain what the pop
please use gerrit instead
2014/10/24 18:13:02
Done.
|
| + */ |
| +public class PasswordGenerationAdapter extends BaseAdapter { |
| + private final Context mContext; |
| + private final Delegate mDelegate; |
| + private final List<Integer> mViews; |
| + private final String mPassword; |
| + private final String mSuggestionTitle; |
| + private final String mExplanationText; |
| + private final int mExplanationTextLinkRangeStart; |
| + private final int mExplanationTextLinkRangeEnd; |
| + private final int mSuggestionMeasuredWidth; |
| + |
| + /** |
| + * 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; |
| + |
| + /** |
| + * There're 2 types of views: SUGGESTION and EXPLANATION. |
| + */ |
| + private static final int VIEW_TYPE_COUNT = 2; |
| + |
| + /** |
| + * 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 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. |
| + * @param anchorWidthInDp The width of the anchor to which the popup is attached. Used to size |
| + * the explanation view. |
| + */ |
| + public PasswordGenerationAdapter(Context context, Delegate delegate, boolean passwordDisplayed, |
| + String password, String suggestionTitle, String explanationText, |
| + int explanationTextLinkRangeStart, int explanationTextLinkRangeEnd, |
| + float anchorWidthInDp) { |
| + super(); |
| + mContext = context; |
| + mDelegate = delegate; |
| + mViews = passwordDisplayed ? Arrays.asList(SUGGESTION, EXPLANATION) : Arrays |
|
aurimas (slooooooooow)
2014/10/23 00:57:01
Style nit: I would move ": Arrays" to the next lin
please use gerrit instead
2014/10/24 18:13:02
Done. I use Android Java style in eclim to format
|
| + .asList(EXPLANATION); |
| + mPassword = password; |
| + mSuggestionTitle = suggestionTitle; |
| + mExplanationText = explanationText; |
| + mExplanationTextLinkRangeStart = explanationTextLinkRangeStart; |
| + mExplanationTextLinkRangeEnd = explanationTextLinkRangeEnd; |
| + |
| + View suggestion = getViewForType(SUGGESTION).findViewById( |
| + R.id.password_generation_suggestion); |
| + int anchorWidthInPx = Math.round(anchorWidthInDp |
| + * mContext.getResources().getDisplayMetrics().density); |
| + suggestion.setMinimumWidth(anchorWidthInPx); |
| + suggestion.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
| + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| + mSuggestionMeasuredWidth = suggestion.getMeasuredWidth(); |
| + } |
| + |
| + /** |
| + * 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) { |
| + return convertView != null ? convertView : getViewForType(mViews.get(position)); |
| + } |
| + |
| + /** |
| + * Builds the view of this type. |
| + * @param type The type of view to build. |
| + * @return The view for this viewType. |
| + */ |
| + private View getViewForType(int type) { |
| + LayoutInflater inflater = (LayoutInflater) mContext |
| + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| + View view = null; |
| + switch (type) { |
| + case SUGGESTION: |
| + view = inflater.inflate(R.layout.password_generation_popup_suggestion, null); |
| + ((TextView) view.findViewById(R.id.password_generation_title)) |
| + .setText(mSuggestionTitle); |
| + ((TextView) view.findViewById(R.id.password_generation_password)) |
| + .setText(mPassword); |
| + break; |
| + |
| + case EXPLANATION: |
| + view = inflater.inflate(R.layout.password_generation_popup_explanation, null); |
| + TextView explanation = (TextView) view |
| + .findViewById(R.id.password_generation_explanation); |
| + SpannableString explanationSpan = new SpannableString(mExplanationText); |
| + explanationSpan.setSpan( |
| + new ClickableSpan() { |
| + @Override |
| + public void onClick(View view) { |
| + mDelegate.onSavedPasswordsLinkClicked(); |
| + } |
| + |
| + @Override |
| + public void updateDrawState(TextPaint textPaint) { |
| + textPaint.setUnderlineText(false); |
|
aurimas (slooooooooow)
2014/10/23 00:57:01
Is it true that we really don't want the underline
please use gerrit instead
2014/10/24 18:13:02
Yes, UX explicitly requested no underline when I f
|
| + textPaint.setColor(mContext.getResources().getColor( |
| + R.color.password_generation_link_text_color)); |
| + } |
| + }, mExplanationTextLinkRangeStart, mExplanationTextLinkRangeEnd, |
| + Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
| + explanation.setText(explanationSpan); |
| + explanation.setMovementMethod(LinkMovementMethod.getInstance()); |
| + explanation.setLayoutParams(new LayoutParams(mSuggestionMeasuredWidth, |
| + LayoutParams.WRAP_CONTENT)); |
| + break; |
| + |
| + default: |
| + assert false : "Unknown view type"; |
| + break; |
| + } |
| + |
| + return view; |
| + } |
| + |
| + @Override |
| + public Integer getItem(int position) { |
| + return mViews.get(position); |
| + } |
| + |
| + @Override |
| + public long getItemId(int position) { |
| + return position; |
| + } |
| + |
| + @Override |
| + public int getItemViewType(int position) { |
| + return mViews.get(position); |
| + } |
| + |
| + @Override |
| + public int getViewTypeCount() { |
| + return VIEW_TYPE_COUNT; |
| + } |
| + |
| + @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 mViews.get(position) == SUGGESTION; |
| + } |
| +} |