Commit de19ad74 authored by wychen's avatar wychen Committed by Commit bot

Avoid creating new CheckedTextViews in AlertDialog for <select>

BUG=649795

Review-Url: https://codereview.chromium.org/2381063003
Cr-Commit-Position: refs/heads/master@{#422690}
parent c093e13f
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.content.browser.input; package org.chromium.content.browser.input;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
...@@ -49,27 +50,36 @@ public class SelectPopupAdapter extends ArrayAdapter<SelectPopupItem> { ...@@ -49,27 +50,36 @@ public class SelectPopupAdapter extends ArrayAdapter<SelectPopupItem> {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
if (position < 0 || position >= getCount()) return null; if (position < 0 || position >= getCount()) return null;
// Always pass in null so that we will get a new CheckedTextView. Otherwise, an item convertView = super.getView(position, convertView, parent);
// which was previously used as an <optgroup> element (i.e. has no check), could get
// used as an <option> element, which needs a checkbox/radio, but it would not have
// one.
convertView = super.getView(position, null, parent);
((TextView) convertView).setText(mItems.get(position).getLabel()); ((TextView) convertView).setText(mItems.get(position).getLabel());
if (mItems.get(position).getType() != PopupItemType.ENABLED) { // Currently select_dialog_(single|multi)choice uses CheckedTextViews.
if (mItems.get(position).getType() == PopupItemType.GROUP) {
// Currently select_dialog_multichoice uses CheckedTextViews.
// If that changes, the class cast will no longer be valid. // If that changes, the class cast will no longer be valid.
// The WebView build cannot rely on this being the case, so // The WebView build cannot rely on this being the case, so
// we must check. // we must check.
if (convertView instanceof CheckedTextView) { if (convertView instanceof CheckedTextView) {
((CheckedTextView) convertView).setCheckMarkDrawable(null); // <optgroup> elements do not have check marks. If an item previously used as an
// <optgroup> gets reused for a non-<optgroup> element, we need to get the check mark
// back. Inflating a new View from XML can be slow, for both the inflation part and GC
// afterwards. Even creating a new Drawable can be tricky, considering getting the
// check/radio type and theme right.
// Saving the previously removed Drawables and reuse them when needed is faster,
// and the memory implication should be fine.
CheckedTextView view = (CheckedTextView) convertView;
if (mItems.get(position).getType() == PopupItemType.GROUP) {
if (view.getCheckMarkDrawable() != null) {
view.setTag(view.getCheckMarkDrawable());
view.setCheckMarkDrawable(null);
} }
} else { } else {
// Draw the disabled element in a disabled state. if (view.getCheckMarkDrawable() == null) {
convertView.setEnabled(false); view.setCheckMarkDrawable((Drawable) view.getTag());
} }
} }
}
// Draw the disabled element in a disabled state.
convertView.setEnabled(mItems.get(position).getType() != PopupItemType.DISABLED);
return convertView; return convertView;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment