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.content.Context; 20 import 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 */ 26 public 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 IP format ###.###.###.### from a packed integer containing 99 * the IP address. The IP address is expected to be in little-endian format (LSB first). That 100 * is, 0x01020304 will return "4.3.2.1". 101 * 102 * @param ipv4Address the IP address as a packed integer with LSB first. 103 * @return string with canonical IP address format. 104 * 105 * @deprecated this method doesn't support IPv6 addresses. Prefer {@link 106 * java.net.InetAddress#getHostAddress()}, which supports both IPv4 and 107 * IPv6 addresses. 108 */ 109 @Deprecated 110 public static String formatIpAddress(int ipv4Address) { 111 return NetworkUtils.intToInetAddress(ipv4Address).getHostAddress(); 112 } 113 } 114