Commit e10f8efa authored by Anthony Vallee-Dubois's avatar Anthony Vallee-Dubois Committed by Commit Bot

Format the language list in Explicit Ask Prompt

This patch aligns the checkboxes to the first text line and adds a
separator between the geo/accept languages and the rest of the list.

Bug: 887678
Change-Id: Icf0f20a2f550541cd940d134b752d0d5deee6f11
Reviewed-on: https://chromium-review.googlesource.com/1237178Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: anthonyvd <anthonyvd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593666}
parent f8f15ef8
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Use of this source code is governed by a BSD-style license that can be Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. --> found in the LICENSE file. -->
<LinearLayout <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
...@@ -11,19 +11,13 @@ ...@@ -11,19 +11,13 @@
style="@style/ListItemContainer" style="@style/ListItemContainer"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:baselineAligned="false"> android:baselineAligned="true">
<CheckBox <CheckBox
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/language_ask_checkbox" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/language_ask_checkbox"
android:orientation="vertical" android:layout_alignBaseline="@+id/ui_language_representation" />
android:layout_gravity="center_vertical" >
<TextView <TextView
android:id="@+id/ui_language_representation" android:id="@+id/ui_language_representation"
...@@ -31,7 +25,8 @@ ...@@ -31,7 +25,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="@style/BlackTitle1" android:textAppearance="@style/BlackTitle1"
android:ellipsize="end" android:ellipsize="end"
android:singleLine="true" /> android:singleLine="true"
android:layout_toEndOf="@+id/language_ask_checkbox" />
<TextView <TextView
android:id="@+id/native_language_representation" android:id="@+id/native_language_representation"
...@@ -39,7 +34,7 @@ ...@@ -39,7 +34,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="@style/BlackBody" android:textAppearance="@style/BlackBody"
android:ellipsize="end" android:ellipsize="end"
android:singleLine="true" /> android:singleLine="true"
android:layout_alignStart="@+id/ui_language_representation"
</LinearLayout> android:layout_below="@+id/ui_language_representation" />
</LinearLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 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. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/language_ask_row"
style="@style/ListItemContainer"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:baselineAligned="true">
<View
style="@style/HorizontalDivider"
android:layout_marginEnd="@dimen/list_item_default_margin" />
</LinearLayout>
...@@ -37,6 +37,12 @@ import java.util.List; ...@@ -37,6 +37,12 @@ import java.util.List;
* once at browser startup when no other promo or modals are shown. * once at browser startup when no other promo or modals are shown.
*/ */
public class LanguageAskPrompt implements ModalDialogView.Controller { public class LanguageAskPrompt implements ModalDialogView.Controller {
private class SeparatorViewHolder extends ViewHolder {
SeparatorViewHolder(View view) {
super(view);
}
}
private class LanguageAskPromptRowViewHolder extends ViewHolder { private class LanguageAskPromptRowViewHolder extends ViewHolder {
private TextView mLanguageNameTextView; private TextView mLanguageNameTextView;
private TextView mNativeNameTextView; private TextView mNativeNameTextView;
...@@ -82,48 +88,92 @@ public class LanguageAskPrompt implements ModalDialogView.Controller { ...@@ -82,48 +88,92 @@ public class LanguageAskPrompt implements ModalDialogView.Controller {
} }
} }
private class LanguageItemAdapter extends RecyclerView.Adapter<LanguageAskPromptRowViewHolder> { private class LanguageItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<LanguageItem> mLanguages; private List<LanguageItem> mTopLanguages;
private List<LanguageItem> mBottomLanguages;
private HashSet<String> mLanguagesUpdate; private HashSet<String> mLanguagesUpdate;
private static final int TYPE_LANGUAGE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
/** /**
* @param context The context this item's views will be associated with. * @param context The context this item's views will be associated with.
* @param languagesUpdate The set of language codes to use to know which languages to * @param languagesUpdate The set of language codes to use to know which languages to
* add/remove from the language list. * add/remove from the language list.
*/ */
public LanguageItemAdapter(Context context, HashSet<String> languagesUpdate) { public LanguageItemAdapter(Context context, HashSet<String> languagesUpdate) {
mLanguages = new ArrayList<LanguageItem>(); mTopLanguages = new ArrayList<LanguageItem>();
mBottomLanguages = new ArrayList<LanguageItem>();
mLanguagesUpdate = languagesUpdate; mLanguagesUpdate = languagesUpdate;
} }
@Override @Override
public LanguageAskPromptRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public int getItemViewType(int position) {
if (position == mTopLanguages.size()) {
return TYPE_SEPARATOR;
}
return TYPE_LANGUAGE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_LANGUAGE_ITEM) {
View row = LayoutInflater.from(parent.getContext()) View row = LayoutInflater.from(parent.getContext())
.inflate(R.layout.language_ask_prompt_row, parent, false); .inflate(R.layout.language_ask_prompt_row, parent, false);
return new LanguageAskPromptRowViewHolder(row); return new LanguageAskPromptRowViewHolder(row);
} else if (viewType == TYPE_SEPARATOR) {
return new SeparatorViewHolder(
LayoutInflater.from(parent.getContext())
.inflate(
R.layout.language_ask_prompt_row_separator, parent, false));
}
// NOTREACHED
return null;
} }
@Override @Override
public void onBindViewHolder(LanguageAskPromptRowViewHolder holder, int position) { public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
LanguageItem lang = mLanguages.get(position); if (getItemViewType(position) == TYPE_LANGUAGE_ITEM) {
holder.setLanguage(lang.getDisplayName(), lang.getNativeDisplayName(), lang.getCode(), LanguageItem lang = getLanguageItemAt(position);
mLanguagesUpdate); ((LanguageAskPromptRowViewHolder) holder)
.setLanguage(lang.getDisplayName(), lang.getNativeDisplayName(),
lang.getCode(), mLanguagesUpdate);
}
// No binding necessary for the separator.
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return mLanguages.size(); // Sum of both lists + a separator.
return mTopLanguages.size() + mBottomLanguages.size() + 1;
}
private LanguageItem getLanguageItemAt(int position) {
if (position < mTopLanguages.size()) {
return mTopLanguages.get(position);
} else if (position > mTopLanguages.size()) {
return mBottomLanguages.get(position - mTopLanguages.size() - 1);
}
// NOTREACHED, this would mean the language item at the separator's position is being
// requested
return null;
} }
/** /**
* Sets the list of languages to |languages| and notifies the RecyclerView that the data has * Sets the list of languages to |languages| and notifies the RecyclerView that the data has
* changed. * changed.
* @param languages The new list of languages to be displayed by the RecyclerView. * @param topLanguages The new list of languages to be displayed above the separator.
* @param bottomLanguages The new list of languages to be displayed below the separator.
*/ */
public void setLanguages(List<LanguageItem> languages) { public void setLanguages(
mLanguages.clear(); List<LanguageItem> topLanguages, List<LanguageItem> bottomLanguages) {
mLanguages.addAll(languages); mTopLanguages.clear();
mTopLanguages.addAll(topLanguages);
mBottomLanguages.clear();
mBottomLanguages.addAll(bottomLanguages);
notifyDataSetChanged(); notifyDataSetChanged();
} }
} }
...@@ -203,20 +253,33 @@ public class LanguageAskPrompt implements ModalDialogView.Controller { ...@@ -203,20 +253,33 @@ public class LanguageAskPrompt implements ModalDialogView.Controller {
List<LanguageItem> languages = PrefServiceBridge.getInstance().getChromeLanguageList(); List<LanguageItem> languages = PrefServiceBridge.getInstance().getChromeLanguageList();
LinkedHashSet<String> currentGeoLanguages = LinkedHashSet<String> currentGeoLanguages =
GeoLanguageProviderBridge.getCurrentGeoLanguages(); GeoLanguageProviderBridge.getCurrentGeoLanguages();
Collections.sort(languages, new Comparator<LanguageItem>() {
List<LanguageItem> topLanguages = new ArrayList<LanguageItem>();
List<LanguageItem> bottomLanguages = new ArrayList<LanguageItem>();
for (LanguageItem language : languages) {
if (currentGeoLanguages.contains(language.getCode())
|| mInitialLanguages.contains(language.getCode())) {
topLanguages.add(language);
} else {
bottomLanguages.add(language);
}
}
Collections.sort(topLanguages, new Comparator<LanguageItem>() {
private int computeItemScore(LanguageItem item) { private int computeItemScore(LanguageItem item) {
// Order languages so that the region's languages are on top, followed by the ones // Order languages so that the region's languages are on top, followed by the ones
// already in the user's accept languages, then the remaining languages in // already in the user's accept languages.
// alphabetical order.
if (currentGeoLanguages.contains(item.getCode())) return -2; if (currentGeoLanguages.contains(item.getCode())) return -2;
return mInitialLanguages.contains(item.getCode()) ? -1 : 0; if (mInitialLanguages.contains(item.getCode())) return -1;
return 0;
} }
@Override @Override
public int compare(LanguageItem first, LanguageItem second) { public int compare(LanguageItem first, LanguageItem second) {
return computeItemScore(first) - computeItemScore(second); return computeItemScore(first) - computeItemScore(second);
} }
}); });
adapter.setLanguages(languages);
adapter.setLanguages(topLanguages, bottomLanguages);
mModalDialogManager = activity.getModalDialogManager(); mModalDialogManager = activity.getModalDialogManager();
mDialog = new ModalDialogView(this, params); mDialog = new ModalDialogView(this, params);
......
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