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 @@
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<LinearLayout
<RelativeLayout
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"
......@@ -11,35 +11,30 @@
style="@style/ListItemContainer"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:baselineAligned="false">
android:baselineAligned="true">
<CheckBox
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_weight="1"
android:orientation="vertical"
android:layout_gravity="center_vertical" >
android:id="@+id/language_ask_checkbox"
android:layout_alignBaseline="@+id/ui_language_representation" />
<TextView
<TextView
android:id="@+id/ui_language_representation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/BlackTitle1"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/native_language_representation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/BlackBody"
android:ellipsize="end"
android:singleLine="true" />
android:singleLine="true"
android:layout_toEndOf="@+id/language_ask_checkbox" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/native_language_representation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/BlackBody"
android:ellipsize="end"
android:singleLine="true"
android:layout_alignStart="@+id/ui_language_representation"
android:layout_below="@+id/ui_language_representation" />
</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;
* once at browser startup when no other promo or modals are shown.
*/
public class LanguageAskPrompt implements ModalDialogView.Controller {
private class SeparatorViewHolder extends ViewHolder {
SeparatorViewHolder(View view) {
super(view);
}
}
private class LanguageAskPromptRowViewHolder extends ViewHolder {
private TextView mLanguageNameTextView;
private TextView mNativeNameTextView;
......@@ -82,48 +88,92 @@ public class LanguageAskPrompt implements ModalDialogView.Controller {
}
}
private class LanguageItemAdapter extends RecyclerView.Adapter<LanguageAskPromptRowViewHolder> {
private List<LanguageItem> mLanguages;
private class LanguageItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<LanguageItem> mTopLanguages;
private List<LanguageItem> mBottomLanguages;
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 languagesUpdate The set of language codes to use to know which languages to
* add/remove from the language list.
*/
public LanguageItemAdapter(Context context, HashSet<String> languagesUpdate) {
mLanguages = new ArrayList<LanguageItem>();
mTopLanguages = new ArrayList<LanguageItem>();
mBottomLanguages = new ArrayList<LanguageItem>();
mLanguagesUpdate = languagesUpdate;
}
@Override
public LanguageAskPromptRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext())
.inflate(R.layout.language_ask_prompt_row, parent, false);
public int getItemViewType(int position) {
if (position == mTopLanguages.size()) {
return TYPE_SEPARATOR;
}
return TYPE_LANGUAGE_ITEM;
}
return new LanguageAskPromptRowViewHolder(row);
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_LANGUAGE_ITEM) {
View row = LayoutInflater.from(parent.getContext())
.inflate(R.layout.language_ask_prompt_row, parent, false);
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
public void onBindViewHolder(LanguageAskPromptRowViewHolder holder, int position) {
LanguageItem lang = mLanguages.get(position);
holder.setLanguage(lang.getDisplayName(), lang.getNativeDisplayName(), lang.getCode(),
mLanguagesUpdate);
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_LANGUAGE_ITEM) {
LanguageItem lang = getLanguageItemAt(position);
((LanguageAskPromptRowViewHolder) holder)
.setLanguage(lang.getDisplayName(), lang.getNativeDisplayName(),
lang.getCode(), mLanguagesUpdate);
}
// No binding necessary for the separator.
}
@Override
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
* 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) {
mLanguages.clear();
mLanguages.addAll(languages);
public void setLanguages(
List<LanguageItem> topLanguages, List<LanguageItem> bottomLanguages) {
mTopLanguages.clear();
mTopLanguages.addAll(topLanguages);
mBottomLanguages.clear();
mBottomLanguages.addAll(bottomLanguages);
notifyDataSetChanged();
}
}
......@@ -203,20 +253,33 @@ public class LanguageAskPrompt implements ModalDialogView.Controller {
List<LanguageItem> languages = PrefServiceBridge.getInstance().getChromeLanguageList();
LinkedHashSet<String> currentGeoLanguages =
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) {
// 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
// alphabetical order.
// already in the user's accept languages.
if (currentGeoLanguages.contains(item.getCode())) return -2;
return mInitialLanguages.contains(item.getCode()) ? -1 : 0;
if (mInitialLanguages.contains(item.getCode())) return -1;
return 0;
}
@Override
public int compare(LanguageItem first, LanguageItem second) {
return computeItemScore(first) - computeItemScore(second);
}
});
adapter.setLanguages(languages);
adapter.setLanguages(topLanguages, bottomLanguages);
mModalDialogManager = activity.getModalDialogManager();
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