Commit 88fef656 authored by Hung Vu's avatar Hung Vu Committed by Commit Bot

[Location Suggestion] Update suggested spinner option.

This CL choose the suggested directory option based on available storage space.Test CL will be added later

Bug: 1118207
Change-Id: I70c71a8bba8677a57f2ed871d246a8b59f3c0783
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2406853
Commit-Queue: Hung Vu <vuhung@google.com>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807212}
parent 9defa6fc
...@@ -42,6 +42,7 @@ public class DownloadLocationCustomView ...@@ -42,6 +42,7 @@ public class DownloadLocationCustomView
private Spinner mFileLocation; private Spinner mFileLocation;
private CheckBox mDontShowAgain; private CheckBox mDontShowAgain;
private @DownloadLocationDialogType int mDialogType; private @DownloadLocationDialogType int mDialogType;
private long mTotalBytes;
public DownloadLocationCustomView(Context context, AttributeSet attrs) { public DownloadLocationCustomView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
...@@ -71,6 +72,8 @@ public class DownloadLocationCustomView ...@@ -71,6 +72,8 @@ public class DownloadLocationCustomView
mFileName.setText(suggestedPath.getName()); mFileName.setText(suggestedPath.getName());
mTitle.setText(title); mTitle.setText(title);
mTotalBytes = totalBytes;
switch (dialogType) { switch (dialogType) {
case DownloadLocationDialogType.DEFAULT: case DownloadLocationDialogType.DEFAULT:
// Show a file size subtitle if file size is available. // Show a file size subtitle if file size is available.
...@@ -156,6 +159,9 @@ public class DownloadLocationCustomView ...@@ -156,6 +159,9 @@ public class DownloadLocationCustomView
|| mDialogType == DownloadLocationDialogType.LOCATION_NOT_FOUND) { || mDialogType == DownloadLocationDialogType.LOCATION_NOT_FOUND) {
selectedItemId = mDirectoryAdapter.useFirstValidSelectableItemId(); selectedItemId = mDirectoryAdapter.useFirstValidSelectableItemId();
} }
if (mDialogType == DownloadLocationDialogType.LOCATION_SUGGESTION) {
selectedItemId = mDirectoryAdapter.useSuggestedItemId(mTotalBytes);
}
mFileLocation.setAdapter(mDirectoryAdapter); mFileLocation.setAdapter(mDirectoryAdapter);
mFileLocation.setSelection(selectedItemId); mFileLocation.setSelection(selectedItemId);
......
...@@ -204,6 +204,41 @@ public class DownloadDirectoryAdapter extends ArrayAdapter<Object> { ...@@ -204,6 +204,41 @@ public class DownloadDirectoryAdapter extends ArrayAdapter<Object> {
return 0; return 0;
} }
/**
* Get the ID of the suggested item based on total bytes and threshold.
* @param totalBytes The total bytes of the download file.
* @return ID of the suggested item and the new default location.
*/
public int useSuggestedItemId(long totalBytes) {
double maxSpaceLeft = 0;
int suggestedId = NO_SELECTED_ITEM_ID;
String defaultLocation = DownloadDialogBridge.getDownloadDefaultDirectory();
for (int i = 0; i < getCount(); i++) {
DirectoryOption option = (DirectoryOption) getItem(i);
if (option == null) continue;
if (defaultLocation.equals(option.location)) continue;
double spaceLeft = (double) (option.availableSpace - totalBytes) / option.totalSpace;
// If a larger storage is found, mark it as the suggested option.
if (spaceLeft > maxSpaceLeft) {
maxSpaceLeft = spaceLeft;
suggestedId = i;
}
}
// If there is a suggested option, set it as default directory and return its position.
if (suggestedId != NO_SELECTED_ITEM_ID) {
DirectoryOption suggestedOption = (DirectoryOption) getItem(suggestedId);
mSelectedPosition = suggestedId;
return suggestedId;
}
// Display an option that says there are no available download locations.
adjustErrorDirectoryOption();
return 0;
}
boolean hasAvailableLocations() { boolean hasAvailableLocations() {
return mErrorOptions.isEmpty(); return mErrorOptions.isEmpty();
} }
......
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