Skip to content

Commit

Permalink
Add support for datalist to text input element on Android
Browse files Browse the repository at this point in the history
We want to have a darker color divider between the datalist suggestions and autofill suggestions.

The ListView instance is constructed inside ListPopupWindow so we can't override ListView.drawDivider(), so we draw the divider inside the list item view.

BUG=242455

Review URL: https://codereview.chromium.org/23314003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234194 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
keishi@chromium.org committed Nov 11, 2013
1 parent b4d0eb1 commit 9f03d8d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 30 deletions.
48 changes: 23 additions & 25 deletions ui/android/java/res/layout/autofill_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autofill_menu_text"
android:layout_width="match_parent"
android:layout_height="44dp"
android:minHeight="44dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView android:id="@+id/autofill_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textSize="18sp"
android:includeFontPadding="false"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView android:id="@+id/autofill_sublabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#8b8b8b"
android:ellipsize="end"
android:singleLine="true"
android:includeFontPadding="false"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
android:id="@+id/autofill_menu_text"
android:orientation="vertical"
android:gravity="center_vertical">

<TextView android:id="@+id/autofill_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:singleLine="true"
android:textSize="18sp"
android:includeFontPadding="false"/>
<TextView android:id="@+id/autofill_sublabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:textSize="14sp"
android:textColor="#8b8b8b"
android:ellipsize="end"
android:singleLine="true"
android:includeFontPadding="false"/>
</LinearLayout>
2 changes: 2 additions & 0 deletions ui/android/java/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
-->
<resources>
<color name="color_picker_border_color">#B0B0B0</color>
<color name="autofill_divider_color">#E5E5E5</color>
<color name="autofill_dark_divider_color">#C0C0C0</color>
</resources>

2 changes: 2 additions & 0 deletions ui/android/java/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
of the seek bar icon.
-->
<dimen name="color_picker_gradient_margin">14.5dp</dimen>
<dimen name="autofill_text_height">44dp</dimen>
<dimen name="autofill_text_divider_height">1px</dimen>

<!-- Infobar dimensions -->
<!-- Text size of the InfoBar message. -->
Expand Down
6 changes: 6 additions & 0 deletions ui/android/java/resource_map/org/chromium/ui/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public static final class style {
public static int AutofillPopupWindow;
}
public static final class color {
public static int autofill_dark_divider_color;
public static int autofill_divider_color;
public static int color_picker_border_color;
}
public static final class dimen {
public static int autofill_text_height;
public static int autofill_text_divider_height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2013 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.ui.autofill;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.PixelFormat;

public class AutofillDividerDrawable extends Drawable {

private Paint mPaint;
private Rect mDividerRect;

public AutofillDividerDrawable() {
mPaint = new Paint();
mDividerRect = new Rect();
}

@Override
public void draw(Canvas canvas) {
canvas.drawRect(mDividerRect, mPaint);
}

@Override
public void onBoundsChange(Rect bounds) {
mDividerRect.set(0, 0, bounds.width(), mDividerRect.height());
}

public void setHeight(int height) {
mDividerRect.set(0, 0, mDividerRect.right, height);
}

public void setColor(int color) {
mPaint.setColor(color);
}

@Override
public void setAlpha(int alpha) {
}

@Override
public void setColorFilter(ColorFilter cf) {
}

@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@
package org.chromium.ui.autofill;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.LayerDrawable;
import android.text.TextUtils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.chromium.ui.R;

import org.chromium.base.ApiCompatibilityUtils;

import java.util.ArrayList;
import java.util.Set;

/**
* Autofill suggestion adapter for AutofillWindow.
*/
public class AutofillListAdapter extends ArrayAdapter<AutofillSuggestion> {
private Context mContext;
private Set<Integer> mSeparators;

AutofillListAdapter(Context context, ArrayList<AutofillSuggestion> objects) {
AutofillListAdapter(Context context,
ArrayList<AutofillSuggestion> objects,
Set<Integer> separators) {
super(context, R.layout.autofill_text, objects);
mSeparators = separators;
mContext = context;
}

Expand All @@ -36,10 +46,30 @@ public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.autofill_text, null);
ApiCompatibilityUtils.setBackgroundForView(layout, new AutofillDividerDrawable());
}
TextView labelView = (TextView) layout.findViewById(R.id.autofill_label);
labelView.setText(getItem(position).mLabel);

AutofillDividerDrawable divider = (AutofillDividerDrawable) layout.getBackground();
int height = mContext.getResources().getDimensionPixelSize(R.dimen.autofill_text_height);
if (position == 0) {
divider.setColor(Color.TRANSPARENT);
} else {
int dividerHeight = mContext.getResources().getDimensionPixelSize(
R.dimen.autofill_text_divider_height);
height += dividerHeight;
divider.setHeight(dividerHeight);
if (mSeparators.contains(position)) {
divider.setColor(mContext.getResources().getColor(
R.color.autofill_dark_divider_color));
} else {
divider.setColor(mContext.getResources().getColor(
R.color.autofill_divider_color));
}
}
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height));

TextView sublabelView = (TextView) layout.findViewById(R.id.autofill_sublabel);
CharSequence sublabel = getItem(position).mSublabel;
if (TextUtils.isEmpty(sublabel)) {
Expand Down
19 changes: 16 additions & 3 deletions ui/android/java/src/org/chromium/ui/autofill/AutofillPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.chromium.ui.R;
import org.chromium.ui.ViewAndroidDelegate;
Expand All @@ -29,11 +32,12 @@ public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
* Constants defining types of Autofill suggestion entries.
* Has to be kept in sync with enum in WebAutofillClient.h
*
* Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDClearForm, and
* Not supported: MenuItemIDWarningMessage, MenuItemIDClearForm, and
* MenuItemIDAutofillOptions.
*/
private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0;
private static final int ITEM_ID_PASSWORD_ENTRY = -2;
private static final int ITEM_ID_SEPARATOR_ENTRY = -3;
private static final int ITEM_ID_DATA_LIST_ENTRY = -6;

private static final int TEXT_PADDING_DP = 30;
Expand All @@ -49,6 +53,7 @@ public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
private Paint mLabelViewPaint;
private Paint mSublabelViewPaint;
private OnLayoutChangeListener mLayoutChangeListener;
private List<AutofillSuggestion> mSuggestions;

/**
* An interface to handle the touch interaction with an AutofillPopup object.
Expand Down Expand Up @@ -105,6 +110,7 @@ public void show() {
// An ugly hack to keep the popup from expanding on top of the keyboard.
setInputMethodMode(INPUT_METHOD_NEEDED);
super.show();
getListView().setDividerHeight(0);
}

/**
Expand All @@ -131,16 +137,20 @@ public void setAnchorRect(float x, float y, float width, float height) {
* @param suggestions Autofill suggestion data.
*/
public void show(AutofillSuggestion[] suggestions) {
mSuggestions = new ArrayList<AutofillSuggestion>(Arrays.asList(suggestions));
// Remove the AutofillSuggestions with IDs that are not supported by Android
ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSuggestion>();
HashSet<Integer> separators = new HashSet<Integer>();
for (int i = 0; i < suggestions.length; i++) {
int itemId = suggestions[i].mUniqueId;
if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY ||
itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_LIST_ENTRY) {
cleanedData.add(suggestions[i]);
} else if (itemId == ITEM_ID_SEPARATOR_ENTRY) {
separators.add(cleanedData.size());
}
}
setAdapter(new AutofillListAdapter(mContext, cleanedData));
setAdapter(new AutofillListAdapter(mContext, cleanedData, separators));
// Once the mAnchorRect is resized and placed correctly, it will show the Autofill popup.
mAnchorWidth = Math.max(getDesiredWidth(cleanedData), mAnchorWidth);
mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mAnchorY, mAnchorWidth,
Expand Down Expand Up @@ -208,7 +218,10 @@ private float getDesiredWidth(ArrayList<AutofillSuggestion> data) {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mAutofillCallback.suggestionSelected(position);
AutofillListAdapter adapter = (AutofillListAdapter) parent.getAdapter();
int listIndex = mSuggestions.indexOf(adapter.getItem(position));
assert listIndex > -1;
mAutofillCallback.suggestionSelected(listIndex);
}

}

0 comments on commit 9f03d8d

Please sign in to comment.