Merge "Fix app language show "Und" when all languages in the suggestion list" into tm-dev

This commit is contained in:
Calvin Pan
2022-05-25 02:36:24 +00:00
committed by Android (Google) Code Review
3 changed files with 71 additions and 26 deletions

View File

@@ -27,6 +27,7 @@ import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.Filter; import android.widget.Filter;
import android.widget.Filterable; import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.android.internal.R; import com.android.internal.R;
@@ -104,6 +105,13 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
if (!showHeaders()) { if (!showHeaders()) {
LocaleStore.LocaleInfo item = (LocaleStore.LocaleInfo) getItem(position);
if (item.isSystemLocale()) {
return TYPE_SYSTEM_LANGUAGE_FOR_APP_LANGUAGE_PICKER;
}
if (item.isAppCurrentLocale()) {
return TYPE_CURRENT_LOCALE;
}
return TYPE_LOCALE; return TYPE_LOCALE;
} else { } else {
if (position == 0) { if (position == 0) {
@@ -193,15 +201,11 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
} }
int itemType = getItemViewType(position); int itemType = getItemViewType(position);
View itemView = getNewViewIfNeeded(convertView, parent, itemType, position);
switch (itemType) { switch (itemType) {
case TYPE_HEADER_SUGGESTED: // intentional fallthrough case TYPE_HEADER_SUGGESTED: // intentional fallthrough
case TYPE_HEADER_ALL_OTHERS: case TYPE_HEADER_ALL_OTHERS:
// Covers both null, and "reusing" a wrong kind of view TextView textView = (TextView) itemView;
if (!(convertView instanceof TextView)) {
convertView = mInflater.inflate(R.layout.language_picker_section_header,
parent, false);
}
TextView textView = (TextView) convertView;
if (itemType == TYPE_HEADER_SUGGESTED) { if (itemType == TYPE_HEADER_SUGGESTED) {
setTextTo(textView, R.string.language_picker_section_suggested); setTextTo(textView, R.string.language_picker_section_suggested);
} else { } else {
@@ -215,38 +219,77 @@ public class SuggestedLocaleAdapter extends BaseAdapter implements Filterable {
mDisplayLocale != null ? mDisplayLocale : Locale.getDefault()); mDisplayLocale != null ? mDisplayLocale : Locale.getDefault());
break; break;
case TYPE_SYSTEM_LANGUAGE_FOR_APP_LANGUAGE_PICKER: case TYPE_SYSTEM_LANGUAGE_FOR_APP_LANGUAGE_PICKER:
if (!(convertView instanceof ViewGroup)) { TextView title;
TextView title; if (((LocaleStore.LocaleInfo)getItem(position)).isAppCurrentLocale()) {
if (((LocaleStore.LocaleInfo)getItem(position)).isAppCurrentLocale()) { title = itemView.findViewById(R.id.language_picker_item);
convertView = mInflater.inflate( } else {
R.layout.app_language_picker_current_locale_item, parent, false); title = itemView.findViewById(R.id.locale);
title = convertView.findViewById(R.id.language_picker_item); }
title.setText(R.string.system_locale_title);
break;
case TYPE_CURRENT_LOCALE:
updateTextView(itemView,
itemView.findViewById(R.id.language_picker_item), position);
break;
default:
updateTextView(itemView, itemView.findViewById(R.id.locale), position);
break;
}
return itemView;
}
/** Check if the old view can be reused, otherwise create a new one. */
private View getNewViewIfNeeded(
View convertView, ViewGroup parent, int itemType, int position) {
View updatedView = convertView;
boolean shouldReuseView;
switch (itemType) {
case TYPE_HEADER_SUGGESTED: // intentional fallthrough
case TYPE_HEADER_ALL_OTHERS:
shouldReuseView = convertView instanceof TextView
&& convertView.findViewById(R.id.language_picker_header) != null;
if (!shouldReuseView) {
updatedView = mInflater.inflate(
R.layout.language_picker_section_header, parent, false);
}
break;
case TYPE_SYSTEM_LANGUAGE_FOR_APP_LANGUAGE_PICKER:
if (((LocaleStore.LocaleInfo) getItem(position)).isAppCurrentLocale()) {
shouldReuseView = convertView instanceof LinearLayout
&& convertView.findViewById(R.id.language_picker_item) != null;
if (!shouldReuseView) {
updatedView = mInflater.inflate(
R.layout.app_language_picker_current_locale_item,
parent, false);
addStateDescriptionIntoCurrentLocaleItem(convertView); addStateDescriptionIntoCurrentLocaleItem(convertView);
} else {
convertView = mInflater.inflate(
R.layout.language_picker_item, parent, false);
title = convertView.findViewById(R.id.locale);
} }
title.setText(R.string.system_locale_title); } else {
shouldReuseView = convertView instanceof TextView
&& convertView.findViewById(R.id.locale) != null;
if (!shouldReuseView) {
updatedView = mInflater.inflate(
R.layout.language_picker_item, parent, false);
}
} }
break; break;
case TYPE_CURRENT_LOCALE: case TYPE_CURRENT_LOCALE:
if (!(convertView instanceof ViewGroup)) { shouldReuseView = convertView instanceof LinearLayout
convertView = mInflater.inflate( && convertView.findViewById(R.id.language_picker_item) != null;
if (!shouldReuseView) {
updatedView = mInflater.inflate(
R.layout.app_language_picker_current_locale_item, parent, false); R.layout.app_language_picker_current_locale_item, parent, false);
addStateDescriptionIntoCurrentLocaleItem(convertView); addStateDescriptionIntoCurrentLocaleItem(convertView);
} }
updateTextView(
convertView, convertView.findViewById(R.id.language_picker_item), position);
break; break;
default: default:
// Covers both null, and "reusing" a wrong kind of view shouldReuseView = convertView instanceof TextView
if (!(convertView instanceof ViewGroup)) { && convertView.findViewById(R.id.locale) != null;
convertView = mInflater.inflate(R.layout.language_picker_item, parent, false); if (!shouldReuseView) {
updatedView = mInflater.inflate(R.layout.language_picker_item, parent, false);
} }
updateTextView(convertView, convertView.findViewById(R.id.locale), position); break;
} }
return convertView; return updatedView;
} }
private boolean showHeaders() { private boolean showHeaders() {

View File

@@ -24,4 +24,5 @@
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textColor="?android:attr/colorAccent" android:textColor="?android:attr/colorAccent"
android:textStyle="bold" android:textStyle="bold"
android:id="@+id/language_picker_header"
tools:text="@string/language_picker_section_all"/> tools:text="@string/language_picker_section_all"/>

View File

@@ -4791,6 +4791,7 @@
<java-symbol type="layout" name="app_language_picker_current_locale_item" /> <java-symbol type="layout" name="app_language_picker_current_locale_item" />
<java-symbol type="id" name="system_locale_subtitle" /> <java-symbol type="id" name="system_locale_subtitle" />
<java-symbol type="id" name="language_picker_item" /> <java-symbol type="id" name="language_picker_item" />
<java-symbol type="id" name="language_picker_header" />
<java-symbol type="dimen" name="status_bar_height_default" /> <java-symbol type="dimen" name="status_bar_height_default" />
</resources> </resources>