Formatter.java revision bfe319e06aa56c081d0d94d64a8181291d7f7388
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;
20
21/**
22 * Utility class to aid in formatting common values that are not covered
23 * by the standard java.util.Formatter.
24 */
25public final class Formatter {
26
27    /**
28     * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc
29     *
30     * @param context Context to use to load the localized units
31     * @param number size value to be formated
32     * @return formated string with the number
33     */
34    public static String formatFileSize(Context context, long number) {
35        return formatFileSize(context, number, false);
36    }
37
38    /**
39     * Like {@link #formatFileSize}, but trying to generate shorter numbers
40     * (showing fewer digits of precisin).
41     */
42    public static String formatShortFileSize(Context context, long number) {
43        return formatFileSize(context, number, true);
44    }
45
46    private static String formatFileSize(Context context, long number, boolean shorter) {
47        if (context == null) {
48            return "";
49        }
50
51        float result = number;
52        int suffix = com.android.internal.R.string.byteShort;
53        if (result > 900) {
54            suffix = com.android.internal.R.string.kilobyteShort;
55            result = result / 1024;
56        }
57        if (result > 900) {
58            suffix = com.android.internal.R.string.megabyteShort;
59            result = result / 1024;
60        }
61        if (result > 900) {
62            suffix = com.android.internal.R.string.gigabyteShort;
63            result = result / 1024;
64        }
65        if (result > 900) {
66            suffix = com.android.internal.R.string.terabyteShort;
67            result = result / 1024;
68        }
69        if (result > 900) {
70            suffix = com.android.internal.R.string.petabyteShort;
71            result = result / 1024;
72        }
73        String value;
74        if (result < 1) {
75            value = String.format("%.2f", result);
76        } else if (result < 10) {
77            if (shorter) {
78                value = String.format("%.1f", result);
79            } else {
80                value = String.format("%.2f", result);
81            }
82        } else if (result < 100) {
83            if (shorter) {
84                value = String.format("%.0f", result);
85            } else {
86                value = String.format("%.2f", result);
87            }
88        } else {
89            value = String.format("%.0f", result);
90        }
91        return context.getResources().
92            getString(com.android.internal.R.string.fileSizeSuffix,
93                      value, context.getString(suffix));
94    }
95
96    /**
97     * Returns a string in the canonical IP format ###.###.###.### from a packed integer containing
98     * the IP address.  The IP address is expected to be in little-endian format (LSB first). That
99     * is, 0x01020304 will return "4.3.2.1".
100     *
101     * @param addr the IP address as a packed integer with LSB first.
102     * @return string with canonical IP address format.
103     */
104    public static String formatIpAddress(int addr) {
105        StringBuffer buf = new StringBuffer();
106        buf.append(addr  & 0xff).append('.').
107            append((addr >>>= 8) & 0xff).append('.').
108            append((addr >>>= 8) & 0xff).append('.').
109            append((addr >>>= 8) & 0xff);
110        return buf.toString();
111    }
112}
113