Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java

Issue 2721043002: [NTP::Downloads] Scale up type based icons. (Closed)
Patch Set: dfalcantara@ comment. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.download; 5 package org.chromium.chrome.browser.download;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.app.PendingIntent; 8 import android.app.PendingIntent;
9 import android.content.ActivityNotFoundException; 9 import android.content.ActivityNotFoundException;
10 import android.content.Context; 10 import android.content.Context;
11 import android.content.Intent; 11 import android.content.Intent;
12 import android.content.res.ColorStateList; 12 import android.content.res.ColorStateList;
13 import android.graphics.Bitmap; 13 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory; 14 import android.graphics.BitmapFactory;
15 import android.graphics.Color; 15 import android.graphics.Color;
16 import android.net.Uri; 16 import android.net.Uri;
17 import android.os.StrictMode; 17 import android.os.StrictMode;
18 import android.provider.Browser; 18 import android.provider.Browser;
19 import android.support.annotation.IntDef;
19 import android.support.annotation.Nullable; 20 import android.support.annotation.Nullable;
20 import android.support.customtabs.CustomTabsIntent; 21 import android.support.customtabs.CustomTabsIntent;
21 import android.text.TextUtils; 22 import android.text.TextUtils;
22 23
23 import org.chromium.base.ApiCompatibilityUtils; 24 import org.chromium.base.ApiCompatibilityUtils;
24 import org.chromium.base.ApplicationStatus; 25 import org.chromium.base.ApplicationStatus;
25 import org.chromium.base.ContentUriUtils; 26 import org.chromium.base.ContentUriUtils;
26 import org.chromium.base.ContextUtils; 27 import org.chromium.base.ContextUtils;
27 import org.chromium.base.Log; 28 import org.chromium.base.Log;
28 import org.chromium.base.VisibleForTesting; 29 import org.chromium.base.VisibleForTesting;
(...skipping 15 matching lines...) Expand all
44 import org.chromium.chrome.browser.tab.Tab; 45 import org.chromium.chrome.browser.tab.Tab;
45 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; 46 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
46 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; 47 import org.chromium.chrome.browser.tabmodel.document.TabDelegate;
47 import org.chromium.chrome.browser.util.IntentUtils; 48 import org.chromium.chrome.browser.util.IntentUtils;
48 import org.chromium.content_public.browser.DownloadState; 49 import org.chromium.content_public.browser.DownloadState;
49 import org.chromium.content_public.browser.LoadUrlParams; 50 import org.chromium.content_public.browser.LoadUrlParams;
50 import org.chromium.ui.base.DeviceFormFactor; 51 import org.chromium.ui.base.DeviceFormFactor;
51 import org.chromium.ui.widget.Toast; 52 import org.chromium.ui.widget.Toast;
52 53
53 import java.io.File; 54 import java.io.File;
55 import java.lang.annotation.Retention;
56 import java.lang.annotation.RetentionPolicy;
54 import java.text.NumberFormat; 57 import java.text.NumberFormat;
55 import java.util.ArrayList; 58 import java.util.ArrayList;
56 import java.util.List; 59 import java.util.List;
57 import java.util.Locale; 60 import java.util.Locale;
58 61
59 /** 62 /**
60 * A class containing some utility static methods. 63 * A class containing some utility static methods.
61 */ 64 */
62 public class DownloadUtils { 65 public class DownloadUtils {
63 66
(...skipping 15 matching lines...) Expand all
79 "org.chromium.chrome.browser.download.IS_OFF_THE_RECORD"; 82 "org.chromium.chrome.browser.download.IS_OFF_THE_RECORD";
80 83
81 private static final long BYTES_PER_KILOBYTE = 1024; 84 private static final long BYTES_PER_KILOBYTE = 1024;
82 private static final long BYTES_PER_MEGABYTE = 1024 * 1024; 85 private static final long BYTES_PER_MEGABYTE = 1024 * 1024;
83 private static final long BYTES_PER_GIGABYTE = 1024 * 1024 * 1024; 86 private static final long BYTES_PER_GIGABYTE = 1024 * 1024 * 1024;
84 87
85 @VisibleForTesting 88 @VisibleForTesting
86 static final String ELLIPSIS = "\u2026"; 89 static final String ELLIPSIS = "\u2026";
87 90
88 /** 91 /**
92 * Possible sizes of type-based icons.
93 */
94 @IntDef({ICON_SIZE_24_DP, ICON_SIZE_36_DP})
95 @Retention(RetentionPolicy.SOURCE)
96 public @interface IconSize {}
97
98 public static final int ICON_SIZE_24_DP = 24;
99 public static final int ICON_SIZE_36_DP = 36;
100
101 /**
89 * Displays the download manager UI. Note the UI is different on tablets and on phones. 102 * Displays the download manager UI. Note the UI is different on tablets and on phones.
90 * @return Whether the UI was shown. 103 * @return Whether the UI was shown.
91 */ 104 */
92 public static boolean showDownloadManager(@Nullable Activity activity, @Null able Tab tab) { 105 public static boolean showDownloadManager(@Nullable Activity activity, @Null able Tab tab) {
93 // Figure out what tab was last being viewed by the user. 106 // Figure out what tab was last being viewed by the user.
94 if (activity == null) activity = ApplicationStatus.getLastTrackedFocused Activity(); 107 if (activity == null) activity = ApplicationStatus.getLastTrackedFocused Activity();
95 if (tab == null && activity instanceof ChromeTabbedActivity) { 108 if (tab == null && activity instanceof ChromeTabbedActivity) {
96 tab = ((ChromeTabbedActivity) activity).getActivityTab(); 109 tab = ((ChromeTabbedActivity) activity).getActivityTab();
97 } 110 }
98 111
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 } else { 655 } else {
643 resourceId = stringSet[2]; 656 resourceId = stringSet[2];
644 bytesInCorrectUnits = bytes / (float) BYTES_PER_GIGABYTE; 657 bytesInCorrectUnits = bytes / (float) BYTES_PER_GIGABYTE;
645 } 658 }
646 659
647 return context.getResources().getString(resourceId, bytesInCorrectUnits) ; 660 return context.getResources().getString(resourceId, bytesInCorrectUnits) ;
648 } 661 }
649 662
650 /** 663 /**
651 * Abbreviate a file name into a given number of characters with ellipses. 664 * Abbreviate a file name into a given number of characters with ellipses.
652 * e.g. thisisaverylongfilename.txt => thisisave....txt 665 * e.g. "thisisaverylongfilename.txt" => "thisisave....txt".
653 * @param fileName File name to abbreviate 666 * @param fileName File name to abbreviate.
654 * @param limit Character limit 667 * @param limit Character limit.
655 * @return Abbreviated file name 668 * @return Abbreviated file name.
656 */ 669 */
657 public static String getAbbreviatedFileName(String fileName, int limit) { 670 public static String getAbbreviatedFileName(String fileName, int limit) {
658 assert limit >= 1; // Abbreviated file name should at least be 1 charac ters (a...) 671 assert limit >= 1; // Abbreviated file name should at least be 1 charac ters (a...)
659 672
660 if (TextUtils.isEmpty(fileName)) return fileName; 673 if (TextUtils.isEmpty(fileName)) return fileName;
661 674
662 if (fileName.length() <= limit) return fileName; 675 if (fileName.length() <= limit) return fileName;
663 676
664 // Find the file name extension 677 // Find the file name extension
665 int index = fileName.lastIndexOf("."); 678 int index = fileName.lastIndexOf(".");
666 int extensionLength = fileName.length() - index; 679 int extensionLength = fileName.length() - index;
667 680
668 // If the extension is too long, just use truncate the string from begin ning. 681 // If the extension is too long, just use truncate the string from begin ning.
669 if (extensionLength >= limit) { 682 if (extensionLength >= limit) {
670 return fileName.substring(0, limit) + ELLIPSIS; 683 return fileName.substring(0, limit) + ELLIPSIS;
671 } 684 }
672 int remainingLength = limit - extensionLength; 685 int remainingLength = limit - extensionLength;
673 return fileName.substring(0, remainingLength) + ELLIPSIS + fileName.subs tring(index); 686 return fileName.substring(0, remainingLength) + ELLIPSIS + fileName.subs tring(index);
674 } 687 }
675 688
676 /** 689 /**
677 * Return an icon for a given file type. 690 * Return an icon for a given file type.
678 * @param fileType Type of the file as returned by DownloadFilter 691 * @param fileType Type of the file as returned by DownloadFilter.
679 * @return Resource ID of the corresponding icon 692 * @param iconSize Size of the returned icon.
693 * @return Resource ID of the corresponding icon.
680 */ 694 */
681 public static int getIconResId(int fileType) { 695 public static int getIconResId(int fileType, @IconSize int iconSize) {
682 switch (fileType) { 696 switch (fileType) {
683 case DownloadFilter.FILTER_PAGE: 697 case DownloadFilter.FILTER_PAGE:
684 return R.drawable.ic_drive_site_white_24dp; 698 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_drive_site_wh ite_24dp
699 : R.drawable.ic_drive_site_wh ite_36dp;
685 case DownloadFilter.FILTER_VIDEO: 700 case DownloadFilter.FILTER_VIDEO:
686 return R.drawable.ic_play_arrow_white_24dp; 701 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_play_arrow_wh ite_24dp
702 : R.drawable.ic_play_arrow_wh ite_36dp;
687 case DownloadFilter.FILTER_AUDIO: 703 case DownloadFilter.FILTER_AUDIO:
688 return R.drawable.ic_music_note_white_24dp; 704 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_music_note_wh ite_24dp
705 : R.drawable.ic_music_note_wh ite_36dp;
689 case DownloadFilter.FILTER_IMAGE: 706 case DownloadFilter.FILTER_IMAGE:
690 return R.drawable.ic_image_white_24dp; 707 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_image_white_2 4dp
708 : R.drawable.ic_image_white_3 6dp;
691 case DownloadFilter.FILTER_DOCUMENT: 709 case DownloadFilter.FILTER_DOCUMENT:
692 return R.drawable.ic_drive_text_white_24dp; 710 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_drive_text_wh ite_24dp
711 : R.drawable.ic_drive_text_wh ite_36dp;
693 default: 712 default:
694 return R.drawable.ic_drive_file_white_24dp; 713 return iconSize == ICON_SIZE_24_DP ? R.drawable.ic_drive_file_wh ite_24dp
714 : R.drawable.ic_drive_text_wh ite_36dp;
Theresa 2017/05/24 21:57:11 Is this supposed to be ic_drive_file_white_36dp?
vitaliii 2017/05/26 09:41:12 Yes (copy-paste mistake). Good catch! Fix: https
695 } 715 }
696 } 716 }
697 717
698 /** 718 /**
699 * Return a background color for the file type icon. 719 * Return a background color for the file type icon.
700 * @param context Context from which to extract the resources 720 * @param context Context from which to extract the resources.
701 * @return Background color 721 * @return Background color.
702 */ 722 */
703 public static int getIconBackgroundColor(Context context) { 723 public static int getIconBackgroundColor(Context context) {
704 return ApiCompatibilityUtils.getColor(context.getResources(), R.color.li ght_active_color); 724 return ApiCompatibilityUtils.getColor(context.getResources(), R.color.li ght_active_color);
705 } 725 }
706 726
707 /** 727 /**
708 * Return a foreground color list for the file type icon. 728 * Return a foreground color list for the file type icon.
709 * @param context Context from which to extract the resources 729 * @param context Context from which to extract the resources.
710 * @return a foreground color list 730 * @return a foreground color list.
711 */ 731 */
712 public static ColorStateList getIconForegroundColorList(Context context) { 732 public static ColorStateList getIconForegroundColorList(Context context) {
713 return ApiCompatibilityUtils.getColorStateList( 733 return ApiCompatibilityUtils.getColorStateList(
714 context.getResources(), R.color.white_mode_tint); 734 context.getResources(), R.color.white_mode_tint);
715 } 735 }
716 736
717 private static boolean isMimeTypeVideo(String mimeType) { 737 private static boolean isMimeTypeVideo(String mimeType) {
718 if (TextUtils.isEmpty(mimeType)) return false; 738 if (TextUtils.isEmpty(mimeType)) return false;
719 739
720 String[] pieces = mimeType.split(MIME_TYPE_DELIMITER); 740 String[] pieces = mimeType.split(MIME_TYPE_DELIMITER);
721 if (pieces.length != 2) return false; 741 if (pieces.length != 2) return false;
722 742
723 return MIME_TYPE_VIDEO.equals(pieces[0]); 743 return MIME_TYPE_VIDEO.equals(pieces[0]);
724 } 744 }
725 } 745 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698