1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.text.format; 18 19import android.content.Context; 20import android.net.NetworkUtils; 21 22/** 23 * Utility class to aid in formatting common values that are not covered 24 * by the {@link java.util.Formatter} class in {@link java.util} 25 */ 26public final class Formatter { 27 28 /** 29 * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc 30 * 31 * @param context Context to use to load the localized units 32 * @param number size value to be formatted 33 * @return formatted string with the number 34 */ 35 public static String formatFileSize(Context context, long number) { 36 return formatFileSize(context, number, false); 37 } 38 39 /** 40 * Like {@link #formatFileSize}, but trying to generate shorter numbers 41 * (showing fewer digits of precision). 42 */ 43 public static String formatShortFileSize(Context context, long number) { 44 return formatFileSize(context, number, true); 45 } 46 47 private static String formatFileSize(Context context, long number, boolean shorter) { 48 if (context == null) { 49 return ""; 50 } 51 52 float result = number; 53 int suffix = com.android.internal.R.string.byteShort; 54 if (result > 900) { 55 suffix = com.android.internal.R.string.kilobyteShort; 56 result = result / 1024; 57 } 58 if (result > 900) { 59 suffix = com.android.internal.R.string.megabyteShort; 60 result = result / 1024; 61 } 62 if (result > 900) { 63 suffix = com.android.internal.R.string.gigabyteShort; 64 result = result / 1024; 65 } 66 if (result > 900) { 67 suffix = com.android.internal.R.string.terabyteShort; 68 result = result / 1024; 69 } 70 if (result > 900) { 71 suffix = com.android.internal.R.string.petabyteShort; 72 result = result / 1024; 73 } 74 String value; 75 if (result < 1) { 76 value = String.format("%.2f", result); 77 } else if (result < 10) { 78 if (shorter) { 79 value = String.format("%.1f", result); 80 } else { 81 value = String.format("%.2f", result); 82 } 83 } else if (result < 100) { 84 if (shorter) { 85 value = String.format("%.0f", result); 86 } else { 87 value = String.format("%.2f", result); 88 } 89 } else { 90 value = String.format("%.0f", result); 91 } 92 return context.getResources(). 93 getString(com.android.internal.R.string.fileSizeSuffix, 94 value, context.getString(suffix)); 95 } 96 97 /** 98 * Returns a string in the canonical IPv4 format ###.###.###.### from a packed integer 99 * containing the IP address. The IPv4 address is expected to be in little-endian 100 * format (LSB first). That is, 0x01020304 will return "4.3.2.1". 101 * 102 * @deprecated Use {@link java.net.InetAddress#getHostAddress()}, which supports both IPv4 and 103 * IPv6 addresses. This method does not support IPv6 addresses. 104 */ 105 @Deprecated 106 public static String formatIpAddress(int ipv4Address) { 107 return NetworkUtils.intToInetAddress(ipv4Address).getHostAddress(); 108 } 109 110 private static final int SECONDS_PER_MINUTE = 60; 111 private static final int SECONDS_PER_HOUR = 60 * 60; 112 private static final int SECONDS_PER_DAY = 24 * 60 * 60; 113 private static final int MILLIS_PER_MINUTE = 1000 * 60; 114 115 /** 116 * Returns elapsed time for the given millis, in the following format: 117 * 1 day 5 hrs; will include at most two units, can go down to seconds precision. 118 * @param context the application context 119 * @param millis the elapsed time in milli seconds 120 * @return the formatted elapsed time 121 * @hide 122 */ 123 public static String formatShortElapsedTime(Context context, long millis) { 124 long secondsLong = millis / 1000; 125 126 int days = 0, hours = 0, minutes = 0; 127 if (secondsLong >= SECONDS_PER_DAY) { 128 days = (int)(secondsLong / SECONDS_PER_DAY); 129 secondsLong -= days * SECONDS_PER_DAY; 130 } 131 if (secondsLong >= SECONDS_PER_HOUR) { 132 hours = (int)(secondsLong / SECONDS_PER_HOUR); 133 secondsLong -= hours * SECONDS_PER_HOUR; 134 } 135 if (secondsLong >= SECONDS_PER_MINUTE) { 136 minutes = (int)(secondsLong / SECONDS_PER_MINUTE); 137 secondsLong -= minutes * SECONDS_PER_MINUTE; 138 } 139 int seconds = (int)secondsLong; 140 141 if (days >= 2) { 142 days += (hours+12)/24; 143 return context.getString(com.android.internal.R.string.durationDays, days); 144 } else if (days > 0) { 145 if (hours == 1) { 146 return context.getString(com.android.internal.R.string.durationDayHour, days, hours); 147 } 148 return context.getString(com.android.internal.R.string.durationDayHours, days, hours); 149 } else if (hours >= 2) { 150 hours += (minutes+30)/60; 151 return context.getString(com.android.internal.R.string.durationHours, hours); 152 } else if (hours > 0) { 153 if (minutes == 1) { 154 return context.getString(com.android.internal.R.string.durationHourMinute, hours, 155 minutes); 156 } 157 return context.getString(com.android.internal.R.string.durationHourMinutes, hours, 158 minutes); 159 } else if (minutes >= 2) { 160 minutes += (seconds+30)/60; 161 return context.getString(com.android.internal.R.string.durationMinutes, minutes); 162 } else if (minutes > 0) { 163 if (seconds == 1) { 164 return context.getString(com.android.internal.R.string.durationMinuteSecond, minutes, 165 seconds); 166 } 167 return context.getString(com.android.internal.R.string.durationMinuteSeconds, minutes, 168 seconds); 169 } else if (seconds == 1) { 170 return context.getString(com.android.internal.R.string.durationSecond, seconds); 171 } else { 172 return context.getString(com.android.internal.R.string.durationSeconds, seconds); 173 } 174 } 175 176 /** 177 * Returns elapsed time for the given millis, in the following format: 178 * 1 day 5 hrs; will include at most two units, can go down to minutes precision. 179 * @param context the application context 180 * @param millis the elapsed time in milli seconds 181 * @return the formatted elapsed time 182 * @hide 183 */ 184 public static String formatShortElapsedTimeRoundingUpToMinutes(Context context, long millis) { 185 long minutesRoundedUp = (millis + MILLIS_PER_MINUTE - 1) / MILLIS_PER_MINUTE; 186 187 if (minutesRoundedUp == 0) { 188 return context.getString(com.android.internal.R.string.durationMinutes, 0); 189 } else if (minutesRoundedUp == 1) { 190 return context.getString(com.android.internal.R.string.durationMinute, 1); 191 } 192 193 return formatShortElapsedTime(context, minutesRoundedUp * MILLIS_PER_MINUTE); 194 } 195} 196