Formatter.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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        if (context == null) {
36            return "";
37        }
38
39        float result = number;
40        int suffix = com.android.internal.R.string.byteShort;
41        if (result > 900) {
42            suffix = com.android.internal.R.string.kilobyteShort;
43            result = result / 1024;
44        }
45        if (result > 900) {
46            suffix = com.android.internal.R.string.megabyteShort;
47            result = result / 1024;
48        }
49        if (result > 900) {
50            suffix = com.android.internal.R.string.gigabyteShort;
51            result = result / 1024;
52        }
53        if (result > 900) {
54            suffix = com.android.internal.R.string.terabyteShort;
55            result = result / 1024;
56        }
57        if (result > 900) {
58            suffix = com.android.internal.R.string.petabyteShort;
59            result = result / 1024;
60        }
61        if (result < 100) {
62            return String.format("%.2f%s", result, context.getText(suffix).toString());
63        }
64        return String.format("%.0f%s", result, context.getText(suffix).toString());
65    }
66
67    /**
68     * Returns a string in the canonical IP format ###.###.###.### from a packed integer containing
69     * the IP address.  The IP address is expected to be in little-endian format (LSB first). That
70     * is, 0x01020304 will return "4.3.2.1".
71     *
72     * @param addr the IP address as a packed integer with LSB first.
73     * @return string with canonical IP address format.
74     */
75    public static String formatIpAddress(int addr) {
76        StringBuffer buf = new StringBuffer();
77        buf.append(addr  & 0xff).append('.').
78            append((addr >>>= 8) & 0xff).append('.').
79            append((addr >>>= 8) & 0xff).append('.').
80            append((addr >>>= 8) & 0xff);
81        return buf.toString();
82    }
83}
84