Commit 6def5d92 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download location: fix radio button double selection.

DownloadLocationPreference directly uses native pref to persist the
selection result. However the native pref may have a delay when
persisting the data, and the UI may refresh the view before native pref
is done, which causes double radio button selection.

This CL caches the selection ID so the UI can always get fresh data.


Bug: 842464,792775
Change-Id: I39bec3a61ea2c7f0e656658cfc41a62e12956560
Reviewed-on: https://chromium-review.googlesource.com/1070480
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561368}
parent 435dd325
......@@ -100,7 +100,7 @@ public class DownloadLocationDialog extends ModalDialogView {
if (selectedItemId == NO_SELECTED_ITEM_ID
|| dialogType == DownloadLocationDialogType.LOCATION_FULL
|| dialogType == DownloadLocationDialogType.LOCATION_NOT_FOUND) {
selectedItemId = mDirectoryAdapter.getFirstSelectableItemId();
selectedItemId = mDirectoryAdapter.useFirstValidSelectableItemId();
}
mFileLocation.setSelection(selectedItemId);
......
......@@ -33,6 +33,9 @@ import java.util.List;
*/
public class DownloadDirectoryAdapter extends ArrayAdapter<Object> {
public static int NO_SELECTED_ITEM_ID = -1;
public static int SELECTED_ITEM_NOT_INITIALIZED = -2;
protected int mSelectedPosition = SELECTED_ITEM_NOT_INITIALIZED;
private Context mContext;
private LayoutInflater mLayoutInflater;
......@@ -150,6 +153,14 @@ public class DownloadDirectoryAdapter extends ArrayAdapter<Object> {
* NO_SELECTED_ITEM_ID if no item matches the default path.
*/
public int getSelectedItemId() {
if (mSelectedPosition == SELECTED_ITEM_NOT_INITIALIZED) {
mSelectedPosition = initSelectedIdFromPref();
}
return mSelectedPosition;
}
private int initSelectedIdFromPref() {
if (!mErrorOptions.isEmpty()) return 0;
String defaultLocation = PrefServiceBridge.getInstance().getDownloadDefaultDirectory();
for (int i = 0; i < getCount(); i++) {
......@@ -167,13 +178,14 @@ public class DownloadDirectoryAdapter extends ArrayAdapter<Object> {
*
* @return ID of the first valid, selectable item and the new default location.
*/
public int getFirstSelectableItemId() {
public int useFirstValidSelectableItemId() {
for (int i = 0; i < getCount(); i++) {
DirectoryOption option = (DirectoryOption) getItem(i);
if (option == null) continue;
if (option.availableSpace > 0) {
PrefServiceBridge.getInstance().setDownloadAndSaveFileDefaultDirectory(
option.location.getAbsolutePath());
mSelectedPosition = i;
return i;
}
}
......
......@@ -35,6 +35,10 @@ public class DownloadLocationPreferenceAdapter
Context context, DownloadLocationPreference preference) {
super(context);
mPreference = preference;
if (getSelectedItemId() == NO_SELECTED_ITEM_ID) {
useFirstValidSelectableItemId();
}
}
@Override
......@@ -48,9 +52,8 @@ public class DownloadLocationPreferenceAdapter
view.setTag(position);
view.setOnClickListener(this);
boolean isSelected = (getSelectedItemId() == position);
RadioButton radioButton = view.findViewById(R.id.radio_button);
radioButton.setChecked(isSelected);
radioButton.setChecked(getSelectedItemId() == position);
view.setEnabled(isEnabled(position));
......@@ -98,6 +101,8 @@ public class DownloadLocationPreferenceAdapter
editor.apply();
mPreference.setSummary(option.location.getAbsolutePath());
mSelectedPosition = selectedId;
// Refresh the list of download directories UI.
notifyDataSetChanged();
}
......
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