| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.Dialog; | 8 import android.app.Dialog; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.DialogInterface; | 10 import android.content.DialogInterface; |
| 11 import android.graphics.Color; | 11 import android.graphics.Color; |
| 12 import android.graphics.drawable.ColorDrawable; | 12 import android.graphics.drawable.ColorDrawable; |
| 13 import android.support.annotation.ColorInt; |
| 14 import android.support.annotation.ColorRes; |
| 13 import android.text.SpannableString; | 15 import android.text.SpannableString; |
| 14 import android.text.method.LinkMovementMethod; | 16 import android.text.method.LinkMovementMethod; |
| 15 import android.view.Gravity; | 17 import android.view.Gravity; |
| 16 import android.view.LayoutInflater; | 18 import android.view.LayoutInflater; |
| 17 import android.view.View; | 19 import android.view.View; |
| 18 import android.view.ViewGroup; | 20 import android.view.ViewGroup; |
| 19 import android.view.ViewGroup.LayoutParams; | 21 import android.view.ViewGroup.LayoutParams; |
| 20 import android.view.Window; | 22 import android.view.Window; |
| 21 import android.widget.AdapterView; | 23 import android.widget.AdapterView; |
| 22 import android.widget.ArrayAdapter; | 24 import android.widget.ArrayAdapter; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 /** | 130 /** |
| 129 * An adapter for keeping track of which items to show in the dialog. | 131 * An adapter for keeping track of which items to show in the dialog. |
| 130 */ | 132 */ |
| 131 public class ItemAdapter extends ArrayAdapter<ItemChooserRow> | 133 public class ItemAdapter extends ArrayAdapter<ItemChooserRow> |
| 132 implements AdapterView.OnItemClickListener { | 134 implements AdapterView.OnItemClickListener { |
| 133 private final LayoutInflater mInflater; | 135 private final LayoutInflater mInflater; |
| 134 | 136 |
| 135 // The background color of the highlighted item. | 137 // The background color of the highlighted item. |
| 136 private final int mBackgroundHighlightColor; | 138 private final int mBackgroundHighlightColor; |
| 137 | 139 |
| 138 // The color of the non-highlighted text. | |
| 139 private final int mDefaultTextColor; | |
| 140 | |
| 141 // The zero-based index of the item currently selected in the dialog, | 140 // The zero-based index of the item currently selected in the dialog, |
| 142 // or -1 (INVALID_POSITION) if nothing is selected. | 141 // or -1 (INVALID_POSITION) if nothing is selected. |
| 143 private int mSelectedItem = ListView.INVALID_POSITION; | 142 private int mSelectedItem = ListView.INVALID_POSITION; |
| 144 | 143 |
| 145 // A set of keys that are marked as disabled in the dialog. | 144 // A set of keys that are marked as disabled in the dialog. |
| 146 private Set<String> mDisabledEntries = new HashSet<String>(); | 145 private Set<String> mDisabledEntries = new HashSet<String>(); |
| 147 | 146 |
| 148 // Item descriptions are counted in a map. | 147 // Item descriptions are counted in a map. |
| 149 private Map<String, Integer> mItemDescriptionMap = new HashMap<>(); | 148 private Map<String, Integer> mItemDescriptionMap = new HashMap<>(); |
| 150 | 149 |
| 151 // Map of keys to items so that we can access the items in O(1). | 150 // Map of keys to items so that we can access the items in O(1). |
| 152 private Map<String, ItemChooserRow> mKeyToItemMap = new HashMap<>(); | 151 private Map<String, ItemChooserRow> mKeyToItemMap = new HashMap<>(); |
| 153 | 152 |
| 154 public ItemAdapter(Context context, int resource) { | 153 public ItemAdapter(Context context, int resource) { |
| 155 super(context, resource); | 154 super(context, resource); |
| 156 | 155 |
| 157 mInflater = LayoutInflater.from(context); | 156 mInflater = LayoutInflater.from(context); |
| 158 | 157 |
| 159 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex
t().getResources(), | 158 mBackgroundHighlightColor = ApiCompatibilityUtils.getColor(getContex
t().getResources(), |
| 160 R.color.light_active_color); | 159 R.color.light_active_color); |
| 161 mDefaultTextColor = ApiCompatibilityUtils.getColor(getContext().getR
esources(), | |
| 162 R.color.default_text_color); | |
| 163 } | 160 } |
| 164 | 161 |
| 165 @Override | 162 @Override |
| 166 public boolean isEmpty() { | 163 public boolean isEmpty() { |
| 167 boolean isEmpty = super.isEmpty(); | 164 boolean isEmpty = super.isEmpty(); |
| 168 if (isEmpty) { | 165 if (isEmpty) { |
| 169 assert mKeyToItemMap.isEmpty(); | 166 assert mKeyToItemMap.isEmpty(); |
| 170 assert mDisabledEntries.isEmpty(); | 167 assert mDisabledEntries.isEmpty(); |
| 171 assert mItemDescriptionMap.isEmpty(); | 168 assert mItemDescriptionMap.isEmpty(); |
| 172 } else { | 169 } else { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 public View getView(int position, View convertView, ViewGroup parent) { | 271 public View getView(int position, View convertView, ViewGroup parent) { |
| 275 TextView view; | 272 TextView view; |
| 276 if (convertView instanceof TextView) { | 273 if (convertView instanceof TextView) { |
| 277 view = (TextView) convertView; | 274 view = (TextView) convertView; |
| 278 } else { | 275 } else { |
| 279 view = (TextView) mInflater.inflate( | 276 view = (TextView) mInflater.inflate( |
| 280 R.layout.item_chooser_dialog_row, parent, false); | 277 R.layout.item_chooser_dialog_row, parent, false); |
| 281 } | 278 } |
| 282 | 279 |
| 283 // Set highlighting for currently selected item. | 280 // Set highlighting for currently selected item. |
| 284 if (position == mSelectedItem) { | 281 final boolean isItemSelected = (position == mSelectedItem); |
| 285 view.setBackgroundColor(mBackgroundHighlightColor); | 282 |
| 286 view.setTextColor(Color.WHITE); | 283 view.setBackgroundColor(isItemSelected ? mBackgroundHighlightColor :
null); |
| 287 } else { | 284 |
| 288 view.setBackground(null); | 285 @ColorInt final int textColor = isEnabled(position) |
| 289 if (!isEnabled(position)) { | 286 ? R.color.default_text_color |
| 290 view.setTextColor(ApiCompatibilityUtils.getColor(getContext(
).getResources(), | 287 : R.color.primary_text_disabled_material_light; |
| 291 R.color.primary_text_disabled_material_light)); | 288 |
| 292 } else { | 289 @ColorRes final int textColorRes = ApiCompatibilityUtils.getColor( |
| 293 view.setTextColor(mDefaultTextColor); | 290 getContext().getResources(), textColor); |
| 294 } | 291 |
| 295 } | 292 view.setTextColor(isItemSelected ? Color.WHITE : textColorRes); |
| 296 | 293 |
| 297 view.setText(getDisplayText(position)); | 294 view.setText(getDisplayText(position)); |
| 298 return view; | 295 return view; |
| 299 } | 296 } |
| 300 | 297 |
| 301 @Override | 298 @Override |
| 302 public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) { | 299 public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) { |
| 303 mSelectedItem = position; | 300 mSelectedItem = position; |
| 304 mConfirmButton.setEnabled(true); | 301 mConfirmButton.setEnabled(true); |
| 305 mItemAdapter.notifyDataSetChanged(); | 302 mItemAdapter.notifyDataSetChanged(); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 } | 544 } |
| 548 | 545 |
| 549 /** | 546 /** |
| 550 * Returns the ItemAdapter associated with this class. For use with tests on
ly. | 547 * Returns the ItemAdapter associated with this class. For use with tests on
ly. |
| 551 */ | 548 */ |
| 552 @VisibleForTesting | 549 @VisibleForTesting |
| 553 public ItemAdapter getItemAdapterForTesting() { | 550 public ItemAdapter getItemAdapterForTesting() { |
| 554 return mItemAdapter; | 551 return mItemAdapter; |
| 555 } | 552 } |
| 556 } | 553 } |
| OLD | NEW |