Commit f366516f authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Move getStringForBytes to components/browser_ui so that it can be shared with WebLayer.

Use it in WebLayer so that its download notifications match Chrome. Also remove DownloadUtils.getStatusString which was unused.

Bug: 1025603
Change-Id: I09d3ff636a750ad32188f8e0c4dddbb84448a7ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2136891Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756777}
parent 97c6133d
...@@ -617,8 +617,9 @@ public class DownloadInfoBarController implements OfflineContentProvider.Observe ...@@ -617,8 +617,9 @@ public class DownloadInfoBarController implements OfflineContentProvider.Observe
R.plurals.download_infobar_downloading_files, inProgressDownloadCount, R.plurals.download_infobar_downloading_files, inProgressDownloadCount,
inProgressDownloadCount); inProgressDownloadCount);
} else { } else {
String bytesString = DownloadUtils.getStringForBytes( String bytesString =
getContext(), totalDownloadingSizeBytes); org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
getContext(), totalDownloadingSizeBytes);
info.message = inProgressDownloadCount == 1 info.message = inProgressDownloadCount == 1
? getContext().getString( ? getContext().getString(
R.string.downloading_file_with_bytes, bytesString) R.string.downloading_file_with_bytes, bytesString)
......
...@@ -166,7 +166,9 @@ public class DownloadLocationDialogBridge implements ModalDialogProperties.Contr ...@@ -166,7 +166,9 @@ public class DownloadLocationDialogBridge implements ModalDialogProperties.Contr
if (totalBytes > 0) { if (totalBytes > 0) {
StringBuilder stringBuilder = new StringBuilder(title); StringBuilder stringBuilder = new StringBuilder(title);
stringBuilder.append(" "); stringBuilder.append(" ");
stringBuilder.append(DownloadUtils.getStringForBytes(mContext, totalBytes)); stringBuilder.append(
org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
mContext, totalBytes));
title = stringBuilder.toString(); title = stringBuilder.toString();
} }
return title; return title;
......
...@@ -244,7 +244,7 @@ public final class DownloadNotificationFactory { ...@@ -244,7 +244,7 @@ public final class DownloadNotificationFactory {
if (downloadUpdate.getTotalBytes() > 0 && !downloadUpdate.getIsOffTheRecord()) { if (downloadUpdate.getTotalBytes() > 0 && !downloadUpdate.getIsOffTheRecord()) {
contentText = context.getResources().getString( contentText = context.getResources().getString(
R.string.download_notification_completed_with_size, R.string.download_notification_completed_with_size,
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, downloadUpdate.getTotalBytes())); context, downloadUpdate.getTotalBytes()));
} else { } else {
contentText = context.getResources().getString( contentText = context.getResources().getString(
......
...@@ -58,7 +58,6 @@ import org.chromium.chrome.browser.tab.TabImpl; ...@@ -58,7 +58,6 @@ import org.chromium.chrome.browser.tab.TabImpl;
import org.chromium.chrome.browser.tab.TabLaunchType; import org.chromium.chrome.browser.tab.TabLaunchType;
import org.chromium.chrome.browser.tabmodel.document.TabDelegate; import org.chromium.chrome.browser.tabmodel.document.TabDelegate;
import org.chromium.chrome.browser.util.AccessibilityUtil; import org.chromium.chrome.browser.util.AccessibilityUtil;
import org.chromium.components.browser_ui.util.ConversionUtils;
import org.chromium.components.download.DownloadState; import org.chromium.components.download.DownloadState;
import org.chromium.components.download.ResumeMode; import org.chromium.components.download.ResumeMode;
import org.chromium.components.embedder_support.util.UrlConstants; import org.chromium.components.embedder_support.util.UrlConstants;
...@@ -104,9 +103,6 @@ public class DownloadUtils { ...@@ -104,9 +103,6 @@ public class DownloadUtils {
R.string.download_manager_ui_space_free_kb, R.string.download_manager_ui_space_free_mb, R.string.download_manager_ui_space_free_kb, R.string.download_manager_ui_space_free_mb,
R.string.download_manager_ui_space_free_gb}; R.string.download_manager_ui_space_free_gb};
private static final int[] BYTES_STRINGS = {
R.string.download_ui_kb, R.string.download_ui_mb, R.string.download_ui_gb};
private static final String TAG = "download"; private static final String TAG = "download";
private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String DEFAULT_MIME_TYPE = "*/*";
...@@ -554,12 +550,16 @@ public class DownloadUtils { ...@@ -554,12 +550,16 @@ public class DownloadUtils {
? context.getResources().getString(R.string.download_started) ? context.getResources().getString(R.string.download_started)
: getPercentageString(progress.getPercentage()); : getPercentageString(progress.getPercentage());
case OfflineItemProgressUnit.BYTES: case OfflineItemProgressUnit.BYTES:
String bytes = getStringForBytes(context, progress.value); String bytes =
org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, progress.value);
if (progress.isIndeterminate()) { if (progress.isIndeterminate()) {
return context.getResources().getString( return context.getResources().getString(
R.string.download_ui_indeterminate_bytes, bytes); R.string.download_ui_indeterminate_bytes, bytes);
} else { } else {
String total = getStringForBytes(context, progress.max); String total =
org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, progress.max);
return context.getResources().getString( return context.getResources().getString(
R.string.download_ui_determinate_bytes, bytes, total); R.string.download_ui_determinate_bytes, bytes, total);
} }
...@@ -697,44 +697,6 @@ public class DownloadUtils { ...@@ -697,44 +697,6 @@ public class DownloadUtils {
return DownloadUtils.getStringForDownloadedBytes(context, bytesReceived); return DownloadUtils.getStringForDownloadedBytes(context, bytesReceived);
} }
/**
* Determine what String to show for a given download in download home.
* @param item Download to check the status of.
* @return String representing the current download status.
*/
public static String getStatusString(DownloadItem item) {
Context context = ContextUtils.getApplicationContext();
DownloadInfo info = item.getDownloadInfo();
Progress progress = info.getProgress();
int state = info.state();
if (state == DownloadState.COMPLETE) {
return context.getString(R.string.download_notification_completed);
}
if (isDownloadPending(item)) {
// All pending, non-offline page downloads are by default waiting for network.
// The other pending reason (i.e. waiting for another download to complete) applies
// only to offline page requests because offline pages download one at a time.
return getPendingStatusString(PendingState.PENDING_NETWORK);
} else if (isDownloadPaused(item)) {
return context.getString(R.string.download_notification_paused);
}
if (info.getBytesReceived() == 0
|| (!item.isIndeterminate() && info.getTimeRemainingInMillis() < 0)) {
// We lack enough information about the download to display a useful string.
return context.getString(R.string.download_started);
} else if (item.isIndeterminate()) {
// Count up the bytes.
long bytes = info.getBytesReceived();
return DownloadUtils.getStringForDownloadedBytes(context, bytes);
} else {
// Count down the time or number of files.
return getTimeOrFilesLeftString(context, progress, info.getTimeRemainingInMillis());
}
}
/** /**
* Determine the status string for a failed download. * Determine the status string for a failed download.
* *
...@@ -836,7 +798,8 @@ public class DownloadUtils { ...@@ -836,7 +798,8 @@ public class DownloadUtils {
* @return A formatted string to be displayed. * @return A formatted string to be displayed.
*/ */
public static String getStringForDownloadedBytes(Context context, long bytes) { public static String getStringForDownloadedBytes(Context context, long bytes) {
return getStringForBytes(context, BYTES_DOWNLOADED_STRINGS, bytes); return org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, BYTES_DOWNLOADED_STRINGS, bytes);
} }
/** /**
...@@ -848,43 +811,8 @@ public class DownloadUtils { ...@@ -848,43 +811,8 @@ public class DownloadUtils {
* @return The formatted string to be displayed. * @return The formatted string to be displayed.
*/ */
public static String getStringForAvailableBytes(Context context, long bytes) { public static String getStringForAvailableBytes(Context context, long bytes) {
return getStringForBytes(context, BYTES_AVAILABLE_STRINGS, bytes); return org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
} context, BYTES_AVAILABLE_STRINGS, bytes);
/**
* Format the number of bytes into KB, MB, or GB and return the corresponding generated string.
* @param context Context to use.
* @param bytes Number of bytes needed to display.
* @return The formatted string to be displayed.
*/
public static String getStringForBytes(Context context, long bytes) {
return getStringForBytes(context, BYTES_STRINGS, bytes);
}
/**
* Format the number of bytes into KB, or MB, or GB and return the corresponding string
* resource.
* @param context Context to use.
* @param stringSet The string resources for displaying bytes in KB, MB and GB.
* @param bytes Number of bytes.
* @return A formatted string to be displayed.
*/
public static String getStringForBytes(Context context, int[] stringSet, long bytes) {
int resourceId;
float bytesInCorrectUnits;
if (ConversionUtils.bytesToMegabytes(bytes) < 1) {
resourceId = stringSet[0];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_KILOBYTE;
} else if (ConversionUtils.bytesToGigabytes(bytes) < 1) {
resourceId = stringSet[1];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_MEGABYTE;
} else {
resourceId = stringSet[2];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_GIGABYTE;
}
return context.getResources().getString(resourceId, bytesInCorrectUnits);
} }
/** /**
......
...@@ -12,9 +12,9 @@ import org.chromium.base.task.AsyncTask; ...@@ -12,9 +12,9 @@ import org.chromium.base.task.AsyncTask;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.download.DirectoryOption; import org.chromium.chrome.browser.download.DirectoryOption;
import org.chromium.chrome.browser.download.DownloadDirectoryProvider; import org.chromium.chrome.browser.download.DownloadDirectoryProvider;
import org.chromium.chrome.browser.download.DownloadUtils;
import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterObserver; import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterObserver;
import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterSource; import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterSource;
import org.chromium.components.browser_ui.util.DownloadUtils;
import org.chromium.components.offline_items_collection.OfflineItem; import org.chromium.components.offline_items_collection.OfflineItem;
import org.chromium.components.offline_items_collection.OfflineItemState; import org.chromium.components.offline_items_collection.OfflineItemState;
......
...@@ -103,19 +103,19 @@ public class DownloadUtilsTest { ...@@ -103,19 +103,19 @@ public class DownloadUtilsTest {
public void testFormatBytesReceived() { public void testFormatBytesReceived() {
final Context context = InstrumentationRegistry.getTargetContext(); final Context context = InstrumentationRegistry.getTargetContext();
Assert.assertEquals("Downloaded 0.0 KB", Assert.assertEquals("Downloaded 0.0 KB",
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 0)); context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 0));
Assert.assertEquals("Downloaded 0.5 KB", Assert.assertEquals("Downloaded 0.5 KB",
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 512)); context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 512));
Assert.assertEquals("Downloaded 1.0 KB", Assert.assertEquals("Downloaded 1.0 KB",
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024)); context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024));
Assert.assertEquals("Downloaded 1.0 MB", Assert.assertEquals("Downloaded 1.0 MB",
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024)); context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024));
Assert.assertEquals("Downloaded 1.0 GB", Assert.assertEquals("Downloaded 1.0 GB",
DownloadUtils.getStringForBytes( org.chromium.components.browser_ui.util.DownloadUtils.getStringForBytes(
context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024 * 1024)); context, DownloadUtils.BYTES_DOWNLOADED_STRINGS, 1024 * 1024 * 1024));
} }
......
...@@ -9,14 +9,22 @@ android_library("java") { ...@@ -9,14 +9,22 @@ android_library("java") {
"java/src/org/chromium/components/browser_ui/util/BrowserControlsVisibilityDelegate.java", "java/src/org/chromium/components/browser_ui/util/BrowserControlsVisibilityDelegate.java",
"java/src/org/chromium/components/browser_ui/util/ComposedBrowserControlsVisibilityDelegate.java", "java/src/org/chromium/components/browser_ui/util/ComposedBrowserControlsVisibilityDelegate.java",
"java/src/org/chromium/components/browser_ui/util/ConversionUtils.java", "java/src/org/chromium/components/browser_ui/util/ConversionUtils.java",
"java/src/org/chromium/components/browser_ui/util/DownloadUtils.java",
] ]
deps = [ deps = [
":java_resources",
"//base:base_java", "//base:base_java",
"//content/public/android:content_java", "//content/public/android:content_java",
] ]
} }
android_resources("java_resources") {
custom_package = "org.chromium.components.browser_ui.util"
sources = []
deps = [ "//components/browser_ui/strings/android:browser_ui_strings_grd" ]
}
static_library("android") { static_library("android") {
sources = [ sources = [
"url_constants.cc", "url_constants.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.browser_ui.util;
import android.content.Context;
/**
* A class containing some utility static methods.
*/
public class DownloadUtils {
private static final int[] BYTES_STRINGS = {
R.string.download_ui_kb, R.string.download_ui_mb, R.string.download_ui_gb};
/**
* Format the number of bytes into KB, MB, or GB and return the corresponding generated string.
* @param context Context to use.
* @param bytes Number of bytes needed to display.
* @return The formatted string to be displayed.
*/
public static String getStringForBytes(Context context, long bytes) {
return getStringForBytes(context, BYTES_STRINGS, bytes);
}
/**
* Format the number of bytes into KB, or MB, or GB and return the corresponding string
* resource.
* @param context Context to use.
* @param stringSet The string resources for displaying bytes in KB, MB and GB.
* @param bytes Number of bytes.
* @return A formatted string to be displayed.
*/
public static String getStringForBytes(Context context, int[] stringSet, long bytes) {
int resourceId;
float bytesInCorrectUnits;
if (ConversionUtils.bytesToMegabytes(bytes) < 1) {
resourceId = stringSet[0];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_KILOBYTE;
} else if (ConversionUtils.bytesToGigabytes(bytes) < 1) {
resourceId = stringSet[1];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_MEGABYTE;
} else {
resourceId = stringSet[2];
bytesInCorrectUnits = bytes / (float) ConversionUtils.BYTES_PER_GIGABYTE;
}
return context.getResources().getString(resourceId, bytesInCorrectUnits);
}
}
...@@ -25,6 +25,7 @@ import org.chromium.base.Log; ...@@ -25,6 +25,7 @@ import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.browser_ui.util.DownloadUtils;
import org.chromium.weblayer_private.interfaces.APICallException; import org.chromium.weblayer_private.interfaces.APICallException;
import org.chromium.weblayer_private.interfaces.DownloadError; import org.chromium.weblayer_private.interfaces.DownloadError;
import org.chromium.weblayer_private.interfaces.DownloadState; import org.chromium.weblayer_private.interfaces.DownloadState;
...@@ -384,12 +385,6 @@ public final class DownloadImpl extends IDownload.Stub { ...@@ -384,12 +385,6 @@ public final class DownloadImpl extends IDownload.Stub {
mBuilder.setContentTitle((new File(location)).getName()); mBuilder.setContentTitle((new File(location)).getName());
} }
if (getTotalBytes() > 0) {
int progressCurrent = (int) (getReceivedBytes() * 100 / getTotalBytes());
mBuilder.setProgress(100, progressCurrent, false);
}
@DownloadState @DownloadState
int state = getState(); int state = getState();
if (state == DownloadState.CANCELLED) { if (state == DownloadState.CANCELLED) {
...@@ -415,15 +410,20 @@ public final class DownloadImpl extends IDownload.Stub { ...@@ -415,15 +410,20 @@ public final class DownloadImpl extends IDownload.Stub {
PendingIntent openPendingIntent = PendingIntent openPendingIntent =
PendingIntent.getBroadcast(context, mNotificationId, openIntent, 0); PendingIntent.getBroadcast(context, mNotificationId, openIntent, 0);
mBuilder.setProgress(100, 100, false) String contextText =
resources.getString(R.string.download_notification_completed_with_size,
DownloadUtils.getStringForBytes(context, getTotalBytes()));
mBuilder.setContentText(contextText)
.setOngoing(false) .setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(openPendingIntent) .setContentIntent(openPendingIntent)
.setAutoCancel(true); .setAutoCancel(true)
.setProgress(0, 0, false);
} else if (state == DownloadState.FAILED) { } else if (state == DownloadState.FAILED) {
mBuilder.setContentText(resources.getString(R.string.download_notification_failed)) mBuilder.setContentText(resources.getString(R.string.download_notification_failed))
.setOngoing(false) .setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done); .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setProgress(0, 0, false);
} else if (state == DownloadState.IN_PROGRESS) { } else if (state == DownloadState.IN_PROGRESS) {
Intent pauseIntent = createIntent(); Intent pauseIntent = createIntent();
pauseIntent.setAction(PAUSE_INTENT); pauseIntent.setAction(PAUSE_INTENT);
...@@ -431,10 +431,29 @@ public final class DownloadImpl extends IDownload.Stub { ...@@ -431,10 +431,29 @@ public final class DownloadImpl extends IDownload.Stub {
pauseIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName); pauseIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
PendingIntent pausePendingIntent = PendingIntent pausePendingIntent =
PendingIntent.getBroadcast(context, mNotificationId, pauseIntent, 0); PendingIntent.getBroadcast(context, mNotificationId, pauseIntent, 0);
mBuilder.addAction(0 /* no icon */,
long bytes = getReceivedBytes();
long totalBytes = getTotalBytes();
boolean indeterminate = totalBytes == -1;
int progressCurrent = -1;
if (!indeterminate) progressCurrent = (int) (bytes * 100 / totalBytes);
String contentText;
String bytesString = DownloadUtils.getStringForBytes(context, bytes);
if (indeterminate) {
contentText =
resources.getString(R.string.download_ui_indeterminate_bytes, bytesString);
} else {
String totalString = DownloadUtils.getStringForBytes(context, totalBytes);
contentText = resources.getString(
R.string.download_ui_determinate_bytes, bytesString, totalString);
}
mBuilder.setContentText(contentText)
.addAction(0 /* no icon */,
resources.getString(R.string.download_notification_pause_button), resources.getString(R.string.download_notification_pause_button),
pausePendingIntent) pausePendingIntent)
.setSmallIcon(android.R.drawable.stat_sys_download); .setSmallIcon(android.R.drawable.stat_sys_download)
.setProgress(100, progressCurrent, indeterminate);
} else if (state == DownloadState.PAUSED) { } else if (state == DownloadState.PAUSED) {
Intent resumeIntent = createIntent(); Intent resumeIntent = createIntent();
resumeIntent.setAction(RESUME_INTENT); resumeIntent.setAction(RESUME_INTENT);
...@@ -442,10 +461,12 @@ public final class DownloadImpl extends IDownload.Stub { ...@@ -442,10 +461,12 @@ public final class DownloadImpl extends IDownload.Stub {
resumeIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName); resumeIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
PendingIntent resumePendingIntent = PendingIntent resumePendingIntent =
PendingIntent.getBroadcast(context, mNotificationId, resumeIntent, 0); PendingIntent.getBroadcast(context, mNotificationId, resumeIntent, 0);
mBuilder.addAction(0 /* no icon */, mBuilder.setContentText(resources.getString(R.string.download_notification_paused))
.addAction(0 /* no icon */,
resources.getString(R.string.download_notification_resume_button), resources.getString(R.string.download_notification_resume_button),
resumePendingIntent) resumePendingIntent)
.setSmallIcon(android.R.drawable.ic_media_pause); .setSmallIcon(android.R.drawable.ic_media_pause)
.setProgress(0, 0, false);
} }
if (state == DownloadState.IN_PROGRESS || state == DownloadState.PAUSED) { if (state == DownloadState.IN_PROGRESS || state == DownloadState.PAUSED) {
......
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