AttachmentUtils.java revision 1a95a57f39da84cd7daf4e2161e50e518df51f13
11a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira/*
21a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * Copyright (C) 2012 The Android Open Source Project
31a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira *
41a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * Licensed under the Apache License, Version 2.0 (the "License");
51a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * you may not use this file except in compliance with the License.
61a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * You may obtain a copy of the License at
71a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira *
81a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira *      http://www.apache.org/licenses/LICENSE-2.0
91a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira *
101a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * Unless required by applicable law or agreed to in writing, software
111a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * distributed under the License is distributed on an "AS IS" BASIS,
121a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * See the License for the specific language governing permissions and
141a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira * limitations under the License.
151a95a57f39da84cd7daf4e2161e50e518df51f13Mindy Pereira */
167b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereirapackage com.android.email.utils;
177b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira
187b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereiraimport android.content.Context;
197b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira
207b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereiraimport com.android.email.R;
217b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira
227b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereiraimport java.text.DecimalFormat;
237b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira
247b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereirapublic class AttachmentUtils {
257b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira    private static final int KILO = 1024;
267b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira    private static final int MEGA = KILO * KILO;
277b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira
287b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira    /**
297b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira     * @return A string suitable for display in bytes, kilobytes or megabytes
307b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira     *         depending on its size.
317b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira     */
327b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira    public static String convertToHumanReadableSize(Context context, long size) {
337b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira        if (size < KILO) {
347b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira            return size + context.getString(R.string.bytes);
357b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira        } else if (size < MEGA) {
367b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira            return (size / KILO) + context.getString(R.string.kilobytes);
377b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira        } else {
387b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira            DecimalFormat onePlace = new DecimalFormat("0.#");
397b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira            return onePlace.format((float) size / (float) MEGA)
407b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira                    + context.getString(R.string.megabytes);
417b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira        }
427b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira    }
437b56a61174eeb202eea468b7f68b79729737ded2Mindy Pereira}
44