AttachmentUtils.java revision 88fc42e48ee4e927eb77e5cab23f2f5151cac649
1/*
2 * Copyright (C) 2012 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 */
16package com.android.mail.utils;
17
18import com.google.common.collect.ImmutableMap;
19
20import android.content.Context;
21
22import com.android.mail.R;
23import com.android.mail.providers.Attachment;
24
25import java.text.DecimalFormat;
26import java.util.Map;
27
28public class AttachmentUtils {
29    private static final int KILO = 1024;
30    private static final int MEGA = KILO * KILO;
31
32    /**
33     * Singleton map of MIME->friendly description
34     * @see #getMimeTypeDisplayName(Context, String)
35     */
36    private static Map<String, String> sDisplayNameMap;
37
38    /**
39     * @return A string suitable for display in bytes, kilobytes or megabytes
40     *         depending on its size.
41     */
42    public static String convertToHumanReadableSize(Context context, long size) {
43        if (size < KILO) {
44            return size + context.getString(R.string.bytes);
45        } else if (size < MEGA) {
46            return (size / KILO) + context.getString(R.string.kilobytes);
47        } else {
48            DecimalFormat onePlace = new DecimalFormat("0.#");
49            return onePlace.format((float) size / (float) MEGA)
50                    + context.getString(R.string.megabytes);
51        }
52    }
53
54    /**
55     * Return a friendly localized file type for this attachment, or the empty string if
56     * unknown.
57     * @param context a Context to do resource lookup against
58     * @return friendly file type or empty string
59     */
60    public static String getDisplayType(final Context context, final Attachment attachment) {
61        // try to get a friendly name for the exact mime type
62        // then try to show a friendly name for the mime family
63        // finally, give up and just show the file extension
64        String displayType = getMimeTypeDisplayName(context, attachment.mimeType);
65        int index = attachment.mimeType.indexOf('/');
66        if (displayType == null && index > 0) {
67            displayType = getMimeTypeDisplayName(context,
68                    attachment.mimeType.substring(0, index));
69        }
70        if (displayType == null) {
71            String extension = Utils.getFileExtension(attachment.name);
72            // show '$EXTENSION File' for unknown file types
73            if (extension != null && extension.length() > 1 && extension.indexOf('.') == 0) {
74                displayType = context.getString(R.string.attachment_unknown,
75                        extension.substring(1).toUpperCase());
76            }
77        }
78        if (displayType == null) {
79         // no extension to display, but the map doesn't accept null entries
80            displayType = "";
81        }
82        return displayType;
83    }
84
85    /**
86     * Returns a user-friendly localized description of either a complete a MIME type or a
87     * MIME family.
88     * @param context used to look up localized strings
89     * @param type complete MIME type or just MIME family
90     * @return localized description text, or null if not recognized
91     */
92    public static synchronized String getMimeTypeDisplayName(final Context context,
93            String type) {
94        if (sDisplayNameMap == null) {
95            String docName = context.getString(R.string.attachment_application_msword);
96            String presoName = context.getString(R.string.attachment_application_vnd_ms_powerpoint);
97            String sheetName = context.getString(R.string.attachment_application_vnd_ms_excel);
98
99            sDisplayNameMap = new ImmutableMap.Builder<String, String>()
100                .put("image", context.getString(R.string.attachment_image))
101                .put("audio", context.getString(R.string.attachment_audio))
102                .put("video", context.getString(R.string.attachment_video))
103                .put("text", context.getString(R.string.attachment_text))
104                .put("application/pdf", context.getString(R.string.attachment_application_pdf))
105
106                // Documents
107                .put("application/msword", docName)
108                .put("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
109                        docName)
110
111                // Presentations
112                .put("application/vnd.ms-powerpoint",
113                        presoName)
114                .put("application/vnd.openxmlformats-officedocument.presentationml.presentation",
115                        presoName)
116
117                // Spreadsheets
118                .put("application/vnd.ms-excel", sheetName)
119                .put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
120                        sheetName)
121
122                .build();
123        }
124        return sDisplayNameMap.get(type);
125    }
126}
127