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