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 17 package android.text.format; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.icu.text.MeasureFormat; 24 import android.icu.util.Measure; 25 import android.icu.util.MeasureUnit; 26 import android.net.NetworkUtils; 27 import android.text.BidiFormatter; 28 import android.text.TextUtils; 29 import android.view.View; 30 31 import java.util.Locale; 32 33 /** 34 * Utility class to aid in formatting common values that are not covered 35 * by the {@link java.util.Formatter} class in {@link java.util} 36 */ 37 public final class Formatter { 38 39 /** {@hide} */ 40 public static final int FLAG_SHORTER = 1 << 0; 41 /** {@hide} */ 42 public static final int FLAG_CALCULATE_ROUNDED = 1 << 1; 43 /** {@hide} */ 44 public static final int FLAG_SI_UNITS = 1 << 2; 45 /** {@hide} */ 46 public static final int FLAG_IEC_UNITS = 1 << 3; 47 48 /** {@hide} */ 49 public static class BytesResult { 50 public final String value; 51 public final String units; 52 public final long roundedBytes; 53 BytesResult(String value, String units, long roundedBytes)54 public BytesResult(String value, String units, long roundedBytes) { 55 this.value = value; 56 this.units = units; 57 this.roundedBytes = roundedBytes; 58 } 59 } 60 localeFromContext(@onNull Context context)61 private static Locale localeFromContext(@NonNull Context context) { 62 return context.getResources().getConfiguration().getLocales().get(0); 63 } 64 65 /* Wraps the source string in bidi formatting characters in RTL locales */ bidiWrap(@onNull Context context, String source)66 private static String bidiWrap(@NonNull Context context, String source) { 67 final Locale locale = localeFromContext(context); 68 if (TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL) { 69 return BidiFormatter.getInstance(true /* RTL*/).unicodeWrap(source); 70 } else { 71 return source; 72 } 73 } 74 75 /** 76 * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc. 77 * 78 * <p>As of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000 79 * bytes, MB = 1,000,000 bytes, etc.</p> 80 * 81 * <p class="note">In {@link android.os.Build.VERSION_CODES#N} and earlier, powers of 1024 are 82 * used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc.</p> 83 * 84 * <p>If the context has a right-to-left locale, the returned string is wrapped in bidi 85 * formatting characters to make sure it's displayed correctly if inserted inside a 86 * right-to-left string. (This is useful in cases where the unit strings, like "MB", are 87 * left-to-right, but the locale is right-to-left.)</p> 88 * 89 * @param context Context to use to load the localized units 90 * @param sizeBytes size value to be formatted, in bytes 91 * @return formatted string with the number 92 */ formatFileSize(@ullable Context context, long sizeBytes)93 public static String formatFileSize(@Nullable Context context, long sizeBytes) { 94 if (context == null) { 95 return ""; 96 } 97 final BytesResult res = formatBytes(context.getResources(), sizeBytes, FLAG_SI_UNITS); 98 return bidiWrap(context, context.getString(com.android.internal.R.string.fileSizeSuffix, 99 res.value, res.units)); 100 } 101 102 /** 103 * Like {@link #formatFileSize}, but trying to generate shorter numbers 104 * (showing fewer digits of precision). 105 */ formatShortFileSize(@ullable Context context, long sizeBytes)106 public static String formatShortFileSize(@Nullable Context context, long sizeBytes) { 107 if (context == null) { 108 return ""; 109 } 110 final BytesResult res = formatBytes(context.getResources(), sizeBytes, 111 FLAG_SI_UNITS | FLAG_SHORTER); 112 return bidiWrap(context, context.getString(com.android.internal.R.string.fileSizeSuffix, 113 res.value, res.units)); 114 } 115 116 /** {@hide} */ formatBytes(Resources res, long sizeBytes, int flags)117 public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) { 118 final int unit = ((flags & FLAG_IEC_UNITS) != 0) ? 1024 : 1000; 119 final boolean isNegative = (sizeBytes < 0); 120 float result = isNegative ? -sizeBytes : sizeBytes; 121 int suffix = com.android.internal.R.string.byteShort; 122 long mult = 1; 123 if (result > 900) { 124 suffix = com.android.internal.R.string.kilobyteShort; 125 mult = unit; 126 result = result / unit; 127 } 128 if (result > 900) { 129 suffix = com.android.internal.R.string.megabyteShort; 130 mult *= unit; 131 result = result / unit; 132 } 133 if (result > 900) { 134 suffix = com.android.internal.R.string.gigabyteShort; 135 mult *= unit; 136 result = result / unit; 137 } 138 if (result > 900) { 139 suffix = com.android.internal.R.string.terabyteShort; 140 mult *= unit; 141 result = result / unit; 142 } 143 if (result > 900) { 144 suffix = com.android.internal.R.string.petabyteShort; 145 mult *= unit; 146 result = result / unit; 147 } 148 // Note we calculate the rounded long by ourselves, but still let String.format() 149 // compute the rounded value. String.format("%f", 0.1) might not return "0.1" due to 150 // floating point errors. 151 final int roundFactor; 152 final String roundFormat; 153 if (mult == 1 || result >= 100) { 154 roundFactor = 1; 155 roundFormat = "%.0f"; 156 } else if (result < 1) { 157 roundFactor = 100; 158 roundFormat = "%.2f"; 159 } else if (result < 10) { 160 if ((flags & FLAG_SHORTER) != 0) { 161 roundFactor = 10; 162 roundFormat = "%.1f"; 163 } else { 164 roundFactor = 100; 165 roundFormat = "%.2f"; 166 } 167 } else { // 10 <= result < 100 168 if ((flags & FLAG_SHORTER) != 0) { 169 roundFactor = 1; 170 roundFormat = "%.0f"; 171 } else { 172 roundFactor = 100; 173 roundFormat = "%.2f"; 174 } 175 } 176 177 if (isNegative) { 178 result = -result; 179 } 180 final String roundedString = String.format(roundFormat, result); 181 182 // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so 183 // it's okay (for now)... 184 final long roundedBytes = 185 (flags & FLAG_CALCULATE_ROUNDED) == 0 ? 0 186 : (((long) Math.round(result * roundFactor)) * mult / roundFactor); 187 188 final String units = res.getString(suffix); 189 190 return new BytesResult(roundedString, units, roundedBytes); 191 } 192 193 /** 194 * Returns a string in the canonical IPv4 format ###.###.###.### from a packed integer 195 * containing the IP address. The IPv4 address is expected to be in little-endian 196 * format (LSB first). That is, 0x01020304 will return "4.3.2.1". 197 * 198 * @deprecated Use {@link java.net.InetAddress#getHostAddress()}, which supports both IPv4 and 199 * IPv6 addresses. This method does not support IPv6 addresses. 200 */ 201 @Deprecated formatIpAddress(int ipv4Address)202 public static String formatIpAddress(int ipv4Address) { 203 return NetworkUtils.intToInetAddress(ipv4Address).getHostAddress(); 204 } 205 206 private static final int SECONDS_PER_MINUTE = 60; 207 private static final int SECONDS_PER_HOUR = 60 * 60; 208 private static final int SECONDS_PER_DAY = 24 * 60 * 60; 209 private static final int MILLIS_PER_MINUTE = 1000 * 60; 210 211 /** 212 * Returns elapsed time for the given millis, in the following format: 213 * 1 day, 5 hr; will include at most two units, can go down to seconds precision. 214 * @param context the application context 215 * @param millis the elapsed time in milli seconds 216 * @return the formatted elapsed time 217 * @hide 218 */ formatShortElapsedTime(Context context, long millis)219 public static String formatShortElapsedTime(Context context, long millis) { 220 long secondsLong = millis / 1000; 221 222 int days = 0, hours = 0, minutes = 0; 223 if (secondsLong >= SECONDS_PER_DAY) { 224 days = (int)(secondsLong / SECONDS_PER_DAY); 225 secondsLong -= days * SECONDS_PER_DAY; 226 } 227 if (secondsLong >= SECONDS_PER_HOUR) { 228 hours = (int)(secondsLong / SECONDS_PER_HOUR); 229 secondsLong -= hours * SECONDS_PER_HOUR; 230 } 231 if (secondsLong >= SECONDS_PER_MINUTE) { 232 minutes = (int)(secondsLong / SECONDS_PER_MINUTE); 233 secondsLong -= minutes * SECONDS_PER_MINUTE; 234 } 235 int seconds = (int)secondsLong; 236 237 final Locale locale = localeFromContext(context); 238 final MeasureFormat measureFormat = MeasureFormat.getInstance( 239 locale, MeasureFormat.FormatWidth.SHORT); 240 if (days >= 2 || (days > 0 && hours == 0)) { 241 days += (hours+12)/24; 242 return measureFormat.format(new Measure(days, MeasureUnit.DAY)); 243 } else if (days > 0) { 244 return measureFormat.formatMeasures( 245 new Measure(days, MeasureUnit.DAY), 246 new Measure(hours, MeasureUnit.HOUR)); 247 } else if (hours >= 2 || (hours > 0 && minutes == 0)) { 248 hours += (minutes+30)/60; 249 return measureFormat.format(new Measure(hours, MeasureUnit.HOUR)); 250 } else if (hours > 0) { 251 return measureFormat.formatMeasures( 252 new Measure(hours, MeasureUnit.HOUR), 253 new Measure(minutes, MeasureUnit.MINUTE)); 254 } else if (minutes >= 2 || (minutes > 0 && seconds == 0)) { 255 minutes += (seconds+30)/60; 256 return measureFormat.format(new Measure(minutes, MeasureUnit.MINUTE)); 257 } else if (minutes > 0) { 258 return measureFormat.formatMeasures( 259 new Measure(minutes, MeasureUnit.MINUTE), 260 new Measure(seconds, MeasureUnit.SECOND)); 261 } else { 262 return measureFormat.format(new Measure(seconds, MeasureUnit.SECOND)); 263 } 264 } 265 266 /** 267 * Returns elapsed time for the given millis, in the following format: 268 * 1 day, 5 hr; will include at most two units, can go down to minutes precision. 269 * @param context the application context 270 * @param millis the elapsed time in milli seconds 271 * @return the formatted elapsed time 272 * @hide 273 */ formatShortElapsedTimeRoundingUpToMinutes(Context context, long millis)274 public static String formatShortElapsedTimeRoundingUpToMinutes(Context context, long millis) { 275 long minutesRoundedUp = (millis + MILLIS_PER_MINUTE - 1) / MILLIS_PER_MINUTE; 276 277 if (minutesRoundedUp == 0 || minutesRoundedUp == 1) { 278 final Locale locale = localeFromContext(context); 279 final MeasureFormat measureFormat = MeasureFormat.getInstance( 280 locale, MeasureFormat.FormatWidth.SHORT); 281 return measureFormat.format(new Measure(minutesRoundedUp, MeasureUnit.MINUTE)); 282 } 283 284 return formatShortElapsedTime(context, minutesRoundedUp * MILLIS_PER_MINUTE); 285 } 286 } 287