Commit fd8b5f0e authored by Hung Vu's avatar Hung Vu Committed by Commit Bot

[Location suggestion] Add condition checking in bridge.

This CL handles when to prompt the suggestion dialog.

Bug: 1118207
Change-Id: Ie0324a029b60554b9e20b4eb9fa2dc127fb590f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2410845
Commit-Queue: Hung Vu <vuhung@google.com>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807220}
parent 504ae7b9
......@@ -12,6 +12,7 @@ import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.download.DownloadLaterMetrics.DownloadLaterUiEvent;
import org.chromium.chrome.browser.download.dialogs.DownloadDateTimePickerDialog;
import org.chromium.chrome.browser.download.dialogs.DownloadDateTimePickerDialogImpl;
import org.chromium.chrome.browser.download.dialogs.DownloadDialogUtils;
import org.chromium.chrome.browser.download.dialogs.DownloadLaterDialogChoice;
import org.chromium.chrome.browser.download.dialogs.DownloadLaterDialogController;
import org.chromium.chrome.browser.download.dialogs.DownloadLaterDialogCoordinator;
......@@ -105,8 +106,17 @@ public class DownloadDialogBridge
mShowEditLocation = (dirs != null && dirs.size() > 1);
ModalDialogManager modalDialogManager =
((ModalDialogManagerHolder) activity).getModalDialogManager();
showDialog(activity, modalDialogManager, getPrefService(), totalBytes, dialogType,
suggestedPath, supportsLaterDialog);
// Suggests an alternative download location.
@DownloadLocationDialogType
int suggestedDialogType = dialogType;
if (ChromeFeatureList.isEnabled(ChromeFeatureList.SMART_SUGGESTION_FOR_LARGE_DOWNLOADS)
&& DownloadDialogUtils.shouldSuggestDownloadLocation(dirs, totalBytes)) {
suggestedDialogType = DownloadLocationDialogType.LOCATION_SUGGESTION;
}
showDialog(activity, modalDialogManager, getPrefService(), totalBytes,
suggestedDialogType, suggestedPath, supportsLaterDialog);
});
}
......
......@@ -4,13 +4,20 @@
package org.chromium.chrome.browser.download.dialogs;
import org.chromium.chrome.browser.download.DirectoryOption;
import org.chromium.chrome.browser.download.DownloadDialogBridge;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.PropertyModel.ReadableObjectPropertyKey;
import java.util.ArrayList;
/**
* Utility functions used in download dialogs.
*/
public class DownloadDialogUtils {
// The threshold to determine if a location suggestion is triggered.
private static final double LOCATION_SUGGESTION_THRESHOLD = 0.05;
/**
* Returns a long value from property model, or a default value.
* @param model The model that contains the data.
......@@ -23,5 +30,27 @@ public class DownloadDialogUtils {
return (value != null) ? value : defaultValue;
}
/**
* Returns whether the download location suggestion dialog should be prompted.
* @param dirs The available directories.
* @param totalBytes The download size.
*/
public static boolean shouldSuggestDownloadLocation(
ArrayList<DirectoryOption> dirs, long totalBytes) {
// Return false if totalBytes is unknown.
if (totalBytes <= 0) return false;
String defaultLocation = DownloadDialogBridge.getDownloadDefaultDirectory();
boolean shouldSuggestDownloadLocation = false;
for (DirectoryOption dir : dirs) {
double spaceLeft = (double) (dir.availableSpace - totalBytes) / dir.totalSpace;
// If not enough space, skip.
if (spaceLeft < LOCATION_SUGGESTION_THRESHOLD) continue;
if (defaultLocation.equals(dir.location)) return false;
shouldSuggestDownloadLocation = true;
}
return shouldSuggestDownloadLocation;
}
private DownloadDialogUtils() {}
}
......@@ -109,7 +109,8 @@ public class DownloadLocationCustomView
case DownloadLocationDialogType.LOCATION_SUGGESTION:
// TODO(vuhung): Add download and storage info to subtitle.
// Right now this subtitle is just a placeholder.
mSubtitleView.setText(DownloadUtils.getStringForBytes(getContext(), totalBytes));
// Putting name too long subtitle here to differentiate with default dialog.
mSubtitleView.setText(R.string.download_location_name_too_long);
break;
}
......
......@@ -973,13 +973,6 @@ void ChromeDownloadManagerDelegate::RequestConfirmation(
DownloadLocationDialogType dialog_type =
DownloadLocationDialogType::DEFAULT;
// Suggests an alternative location.
// TODO(vuhung): Conditions checking will be added in next CL.
if (base::FeatureList::IsEnabled(
download::features::kSmartSuggestionForLargeDownloads)) {
dialog_type = DownloadLocationDialogType::LOCATION_SUGGESTION;
}
switch (reason) {
case DownloadConfirmationReason::TARGET_NO_SPACE:
dialog_type = DownloadLocationDialogType::LOCATION_FULL;
......
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