Fixed bug when turning tablet with no file in the right fragment
[pub/Android/ownCloud.git] / src / com / owncloud / android / DisplayUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android;
20
21 import java.util.Arrays;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import android.util.Log;
28
29 /**
30 * A helper class for some string operations.
31 *
32 * @author Bartek Przybylski
33 * @author David A. Velasco
34 */
35 public class DisplayUtils {
36
37 private static String TAG = DisplayUtils.class.getSimpleName();
38
39 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
40
41 private static HashMap<String, String> mimeType2HUmanReadable;
42 static {
43 mimeType2HUmanReadable = new HashMap<String, String>();
44 // images
45 mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
46 mimeType2HUmanReadable.put("image/jpg", "JPEG image");
47 mimeType2HUmanReadable.put("image/png", "PNG image");
48 mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
49 mimeType2HUmanReadable.put("image/gif", "GIF image");
50 mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
51 mimeType2HUmanReadable.put("image/tiff", "TIFF image");
52 // music
53 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
54 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
55
56 }
57
58 private static final String TYPE_APPLICATION = "application";
59 private static final String TYPE_AUDIO = "audio";
60 private static final String TYPE_IMAGE = "image";
61 private static final String TYPE_TXT = "text";
62 private static final String TYPE_VIDEO = "video";
63
64 private static final String SUBTYPE_PDF = "pdf";
65 private static final String[] SUBTYPES_DOCUMENT = { "msword", "mspowerpoint", "msexcel",
66 "vnd.oasis.opendocument.presentation",
67 "vnd.oasis.opendocument.spreadsheet",
68 "vnd.oasis.opendocument.text"
69 };
70 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
71 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
72 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
73
74 /**
75 * Converts the file size in bytes to human readable output.
76 *
77 * @param bytes Input file size
78 * @return Like something readable like "12 MB"
79 */
80 public static String bytesToHumanReadable(long bytes) {
81 double result = bytes;
82 int attachedsuff = 0;
83 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
84 result /= 1024.;
85 attachedsuff++;
86 }
87 result = ((int) (result * 100)) / 100.;
88 return result + " " + sizeSuffixes[attachedsuff];
89 }
90
91 /**
92 * Removes special HTML entities from a string
93 *
94 * @param s Input string
95 * @return A cleaned version of the string
96 */
97 public static String HtmlDecode(String s) {
98 /*
99 * TODO: Perhaps we should use something more proven like:
100 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
101 */
102
103 String ret = "";
104 for (int i = 0; i < s.length(); ++i) {
105 if (s.charAt(i) == '%') {
106 ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
107 i += 2;
108 } else {
109 ret += s.charAt(i);
110 }
111 }
112 return ret;
113 }
114
115 /**
116 * Converts MIME types like "image/jpg" to more end user friendly output
117 * like "JPG image".
118 *
119 * @param mimetype MIME type to convert
120 * @return A human friendly version of the MIME type
121 */
122 public static String convertMIMEtoPrettyPrint(String mimetype) {
123 if (mimeType2HUmanReadable.containsKey(mimetype)) {
124 return mimeType2HUmanReadable.get(mimetype);
125 }
126 if (mimetype.split("/").length >= 2)
127 return mimetype.split("/")[1].toUpperCase() + " file";
128 return "Unknown type";
129 }
130
131
132 /**
133 * Returns the resource identifier of an image resource to use as icon associated to a
134 * known MIME type.
135 *
136 * @param mimetype MIME type string.
137 * @return Resource identifier of an image resource.
138 */
139 public static int getResourceId(String mimetype) {
140
141 if (mimetype == null || "DIR".equals(mimetype)) {
142 return R.drawable.ic_menu_archive;
143
144 } else {
145 String [] parts = mimetype.split("/");
146 String type = parts[0];
147 String subtype = (parts.length > 1) ? parts[1] : "";
148
149 if(TYPE_TXT.equals(type)) {
150 return R.drawable.file_doc;
151
152 } else if(TYPE_IMAGE.equals(type)) {
153 return R.drawable.file_image;
154
155 } else if(TYPE_VIDEO.equals(type)) {
156 return R.drawable.file_movie;
157
158 } else if(TYPE_AUDIO.equals(type)) {
159 return R.drawable.file_sound;
160
161 } else if(TYPE_APPLICATION.equals(type)) {
162
163 if (SUBTYPE_PDF.equals(subtype)) {
164 return R.drawable.file_pdf;
165
166 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
167 return R.drawable.file_doc;
168
169 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
170 return R.drawable.file_zip;
171 }
172
173 }
174 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
175 }
176
177 // default icon
178 return R.drawable.file;
179 }
180
181
182
183 /**
184 * Converts Unix time to human readable format
185 * @param miliseconds that have passed since 01/01/1970
186 * @return The human readable time for the users locale
187 */
188 public static String unixTimeToHumanReadable(long milliseconds) {
189 Date date = new Date(milliseconds);
190 return date.toLocaleString();
191 }
192 }