[Panlingual] Do not show dialog in recycle. Set LocaleEditor as the

parent fragment and control the dialog.

Keep the dialog and the locale list after rotation

Bug: 279527362
Change-Id: I349fd9d0fea5b43a8bbb2a23fa60fc4c5436c5cf
Test: make RunSettingsRoboTests -j128 ROBOTEST_FILTER=LocaleListEditorTest
Test: atest LocaleDialogFragmentTest
This commit is contained in:
Zoey Chen
2023-05-02 08:47:51 +00:00
parent d2e8495f4c
commit fb6b6b0bcc
6 changed files with 266 additions and 168 deletions

View File

@@ -16,14 +16,11 @@
package com.android.settings.localepicker;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.LocaleList;
import android.os.Looper;
import android.os.ResultReceiver;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
@@ -52,15 +49,20 @@ class LocaleDragAndDropAdapter
private static final String TAG = "LocaleDragAndDropAdapter";
private static final String CFGKEY_SELECTED_LOCALES = "selectedLocales";
private static final String CFGKEY_DRAG_LOCALE = "dragLocales";
private static final String CFGKEY_DRAG_LOCALES_TO_POSITION = "dragLocales_end";
private final Context mContext;
private final ItemTouchHelper mItemTouchHelper;
private List<LocaleStore.LocaleInfo> mFeedItemList;
private List<LocaleStore.LocaleInfo> mCacheItemList;
private final ItemTouchHelper mItemTouchHelper;
private RecyclerView mParentView = null;
private LocaleListEditor mParent;
private boolean mRemoveMode = false;
private boolean mDragEnabled = true;
private NumberFormat mNumberFormatter = NumberFormat.getNumberInstance();
private LocaleStore.LocaleInfo mDragLocale;
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener {
private final LocaleDragCell mLocaleDragCell;
@@ -87,8 +89,7 @@ class LocaleDragAndDropAdapter
}
}
LocaleDragAndDropAdapter(LocaleListEditor parent,
List<LocaleStore.LocaleInfo> feedItemList) {
LocaleDragAndDropAdapter(LocaleListEditor parent, List<LocaleStore.LocaleInfo> feedItemList) {
mFeedItemList = feedItemList;
mParent = parent;
mCacheItemList = new ArrayList<>(feedItemList);
@@ -202,6 +203,7 @@ class LocaleDragAndDropAdapter
final LocaleStore.LocaleInfo saved = mFeedItemList.get(fromPosition);
mFeedItemList.remove(fromPosition);
mFeedItemList.add(toPosition, saved);
mDragLocale = saved;
} else {
// TODO: It looks like sometimes the RecycleView tries to swap item -1
// I did not see it in a while, but if it happens, investigate and file a bug.
@@ -317,43 +319,20 @@ class LocaleDragAndDropAdapter
});
}
public void doTheUpdateWithMovingLocaleItem() {
LocaleStore.LocaleInfo localeInfo = mFeedItemList.get(0);
final LocaleDialogFragment fragment = LocaleDialogFragment.newInstance();
if (!localeInfo.getLocale().equals(LocalePicker.getLocales().get(0))) {
fragment.show(mParent,
LocaleDialogFragment.DIALOG_CONFIRM_SYSTEM_DEFAULT,
localeInfo,
new ResultReceiver(new Handler(Looper.getMainLooper())) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
int type = resultData.getInt(LocaleDialogFragment.ARG_DIALOG_TYPE);
if (type == LocaleDialogFragment.DIALOG_CONFIRM_SYSTEM_DEFAULT) {
if (resultCode == Activity.RESULT_OK) {
doTheUpdate();
if (!localeInfo.isTranslated()) {
fragment.show(mParent,
LocaleDialogFragment
.DIALOG_NOT_AVAILABLE_LOCALE,
localeInfo);
}
} else {
if (!localeInfo.getLocale()
.equals(mCacheItemList.get(0).getLocale())) {
mFeedItemList = new ArrayList<>(mCacheItemList);
notifyDataSetChanged();
}
}
mCacheItemList = new ArrayList<>(mFeedItemList);
}
}
});
} else {
doTheUpdate();
public void notifyListChanged(LocaleStore.LocaleInfo localeInfo) {
if (!localeInfo.getLocale().equals(mCacheItemList.get(0).getLocale())) {
mFeedItemList = new ArrayList<>(mCacheItemList);
notifyDataSetChanged();
}
}
public void setCacheItemList() {
mCacheItemList = new ArrayList<>(mFeedItemList);
}
public List<LocaleStore.LocaleInfo> getFeedItemList() {
return mFeedItemList;
}
private void setDragEnabled(boolean enabled) {
mDragEnabled = enabled;
}
@@ -373,6 +352,8 @@ class LocaleDragAndDropAdapter
}
}
outInstanceState.putStringArrayList(CFGKEY_SELECTED_LOCALES, selectedLocales);
// Save the dragged locale before rotation
outInstanceState.putSerializable(CFGKEY_DRAG_LOCALE, mDragLocale);
}
}
@@ -381,18 +362,30 @@ class LocaleDragAndDropAdapter
* (for instance when the device is rotated)
*
* @param savedInstanceState Bundle with the data saved by {@link #saveState(Bundle)}
* @param isDialogShowing A flag indicating whether the dialog is showing or not.
*/
public void restoreState(Bundle savedInstanceState) {
if (savedInstanceState != null && mRemoveMode) {
final ArrayList<String> selectedLocales =
savedInstanceState.getStringArrayList(CFGKEY_SELECTED_LOCALES);
if (selectedLocales == null || selectedLocales.isEmpty()) {
return;
public void restoreState(Bundle savedInstanceState, boolean isDialogShowing) {
if (savedInstanceState != null) {
if (mRemoveMode) {
final ArrayList<String> selectedLocales =
savedInstanceState.getStringArrayList(CFGKEY_SELECTED_LOCALES);
if (selectedLocales == null || selectedLocales.isEmpty()) {
return;
}
for (LocaleStore.LocaleInfo li : mFeedItemList) {
li.setChecked(selectedLocales.contains(li.getId()));
}
notifyItemRangeChanged(0, mFeedItemList.size());
} else if (isDialogShowing) {
// After rotation, the dragged position will be restored to original. Restore the
// drag locale's original position to the top.
mDragLocale = (LocaleStore.LocaleInfo) savedInstanceState.getSerializable(
CFGKEY_DRAG_LOCALE);
mFeedItemList.removeIf(
localeInfo -> TextUtils.equals(localeInfo.getId(), mDragLocale.getId()));
mFeedItemList.add(0, mDragLocale);
notifyItemRangeChanged(0, mFeedItemList.size());
}
for (LocaleStore.LocaleInfo li : mFeedItemList) {
li.setChecked(selectedLocales.contains(li.getId()));
}
notifyItemRangeChanged(0, mFeedItemList.size());
}
}
}