Merge branch 'develop' of github.com:owncloud/android into switchListVsGrid
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / DisplayUtils.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 package com.owncloud.android.utils;
24
25 import java.io.File;
26 import java.net.IDN;
27 import java.text.DateFormat;
28 import java.util.Arrays;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Set;
34 import java.util.Vector;
35
36 import android.annotation.TargetApi;
37 import android.app.Activity;
38 import android.content.Context;
39 import android.content.SharedPreferences;
40 import android.graphics.Point;
41 import android.graphics.PorterDuff;
42 import android.os.Build;
43 import android.text.format.DateUtils;
44 import android.view.Display;
45 import android.webkit.MimeTypeMap;
46 import android.widget.ProgressBar;
47 import android.widget.SeekBar;
48
49 import com.owncloud.android.MainApp;
50 import com.owncloud.android.R;
51 import com.owncloud.android.datamodel.FileDataStorageManager;
52 import com.owncloud.android.datamodel.OCFile;
53
54 /**
55 * A helper class for some string operations.
56 */
57 public class DisplayUtils {
58
59 private static final String OWNCLOUD_APP_NAME = "ownCloud";
60
61 //private static String TAG = DisplayUtils.class.getSimpleName();
62
63 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
64
65 private static HashMap<String, String> mimeType2HUmanReadable;
66 static {
67 mimeType2HUmanReadable = new HashMap<String, String>();
68 // images
69 mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
70 mimeType2HUmanReadable.put("image/jpg", "JPEG image");
71 mimeType2HUmanReadable.put("image/png", "PNG image");
72 mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
73 mimeType2HUmanReadable.put("image/gif", "GIF image");
74 mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
75 mimeType2HUmanReadable.put("image/tiff", "TIFF image");
76 // music
77 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
78 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
79
80 }
81
82 private static final String TYPE_APPLICATION = "application";
83 private static final String TYPE_AUDIO = "audio";
84 private static final String TYPE_IMAGE = "image";
85 private static final String TYPE_TXT = "text";
86 private static final String TYPE_VIDEO = "video";
87
88 private static final String SUBTYPE_PDF = "pdf";
89 private static final String SUBTYPE_XML = "xml";
90 private static final String[] SUBTYPES_DOCUMENT = {
91 "msword",
92 "vnd.openxmlformats-officedocument.wordprocessingml.document",
93 "vnd.oasis.opendocument.text",
94 "rtf",
95 "javascript"
96 };
97 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
98 private static final String[] SUBTYPES_SPREADSHEET = {
99 "msexcel",
100 "vnd.ms-excel",
101 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
102 "vnd.oasis.opendocument.spreadsheet"
103 };
104 private static Set<String> SUBTYPES_SPREADSHEET_SET = new HashSet<String>(Arrays.asList(SUBTYPES_SPREADSHEET));
105 private static final String[] SUBTYPES_PRESENTATION = {
106 "mspowerpoint",
107 "vnd.ms-powerpoint",
108 "vnd.openxmlformats-officedocument.presentationml.presentation",
109 "vnd.oasis.opendocument.presentation"
110 };
111 private static Set<String> SUBTYPES_PRESENTATION_SET = new HashSet<String>(Arrays.asList(SUBTYPES_PRESENTATION));
112 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
113 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
114 private static final String SUBTYPE_OCTET_STREAM = "octet-stream";
115 private static final String EXTENSION_RAR = "rar";
116 private static final String EXTENSION_RTF = "rtf";
117 private static final String EXTENSION_3GP = "3gp";
118 private static final String EXTENSION_PY = "py";
119 private static final String EXTENSION_JS = "js";
120
121 /**
122 * Converts the file size in bytes to human readable output.
123 *
124 * @param bytes Input file size
125 * @return Like something readable like "12 MB"
126 */
127 public static String bytesToHumanReadable(long bytes) {
128 double result = bytes;
129 int attachedsuff = 0;
130 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
131 result /= 1024.;
132 attachedsuff++;
133 }
134 result = ((int) (result * 100)) / 100.;
135 return result + " " + sizeSuffixes[attachedsuff];
136 }
137
138 /**
139 * Converts MIME types like "image/jpg" to more end user friendly output
140 * like "JPG image".
141 *
142 * @param mimetype MIME type to convert
143 * @return A human friendly version of the MIME type
144 */
145 public static String convertMIMEtoPrettyPrint(String mimetype) {
146 if (mimeType2HUmanReadable.containsKey(mimetype)) {
147 return mimeType2HUmanReadable.get(mimetype);
148 }
149 if (mimetype.split("/").length >= 2)
150 return mimetype.split("/")[1].toUpperCase() + " file";
151 return "Unknown type";
152 }
153
154
155 /**
156 * Returns the resource identifier of an image to use as icon associated to a known MIME type.
157 *
158 * @param mimetype MIME type string; if NULL, the method tries to guess it from the extension in filename
159 * @param filename Name, with extension.
160 * @return Identifier of an image resource.
161 */
162 public static int getFileTypeIconId(String mimetype, String filename) {
163
164 if (mimetype == null) {
165 String fileExtension = getExtension(filename);
166 mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
167 if (mimetype == null) {
168 mimetype = TYPE_APPLICATION + "/" + SUBTYPE_OCTET_STREAM;
169 }
170 }
171
172 if ("DIR".equals(mimetype)) {
173 return R.drawable.ic_menu_archive;
174
175 } else {
176 String [] parts = mimetype.split("/");
177 String type = parts[0];
178 String subtype = (parts.length > 1) ? parts[1] : "";
179
180 if(TYPE_TXT.equals(type)) {
181 return R.drawable.file_doc;
182
183 } else if(TYPE_IMAGE.equals(type)) {
184 return R.drawable.file_image;
185
186 } else if(TYPE_VIDEO.equals(type)) {
187 return R.drawable.file_movie;
188
189 } else if(TYPE_AUDIO.equals(type)) {
190 return R.drawable.file_sound;
191
192 } else if(TYPE_APPLICATION.equals(type)) {
193
194 if (SUBTYPE_PDF.equals(subtype)) {
195 return R.drawable.file_pdf;
196
197 } else if (SUBTYPE_XML.equals(subtype)) {
198 return R.drawable.file_doc;
199
200 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
201 return R.drawable.file_doc;
202
203 } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) {
204 return R.drawable.file_xls;
205
206 } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) {
207 return R.drawable.file_ppt;
208
209 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
210 return R.drawable.file_zip;
211
212 } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) {
213 if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) {
214 return R.drawable.file_zip;
215
216 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) {
217 return R.drawable.file_doc;
218
219 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) {
220 return R.drawable.file_movie;
221
222 } else if ( getExtension(filename).equalsIgnoreCase(EXTENSION_PY) ||
223 getExtension(filename).equalsIgnoreCase(EXTENSION_JS)) {
224 return R.drawable.file_doc;
225 }
226 }
227 }
228 }
229
230 // default icon
231 return R.drawable.file;
232 }
233
234
235 private static String getExtension(String filename) {
236 String extension = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
237 return extension;
238 }
239
240 /**
241 * Converts Unix time to human readable format
242 * @param milliseconds that have passed since 01/01/1970
243 * @return The human readable time for the users locale
244 */
245 public static String unixTimeToHumanReadable(long milliseconds) {
246 Date date = new Date(milliseconds);
247 DateFormat df = DateFormat.getDateTimeInstance();
248 return df.format(date);
249 }
250
251
252 public static int getSeasonalIconId() {
253 if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354 &&
254 MainApp.getAppContext().getString(R.string.app_name).equals(OWNCLOUD_APP_NAME)) {
255 return R.drawable.winter_holidays_icon;
256 } else {
257 return R.drawable.icon;
258 }
259 }
260
261 /**
262 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
263 * @param url the URL where the domain name should be converted
264 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
265 * @return the URL containing the converted domain name
266 */
267 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
268 public static String convertIdn(String url, boolean toASCII) {
269
270 String urlNoDots = url;
271 String dots="";
272 while (urlNoDots.startsWith(".")) {
273 urlNoDots = url.substring(1);
274 dots = dots + ".";
275 }
276
277 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
278 // Find host name after '//' or '@'
279 int hostStart = 0;
280 if (urlNoDots.indexOf("//") != -1) {
281 hostStart = url.indexOf("//") + "//".length();
282 } else if (url.indexOf("@") != -1) {
283 hostStart = url.indexOf("@") + "@".length();
284 }
285
286 int hostEnd = url.substring(hostStart).indexOf("/");
287 // Handle URL which doesn't have a path (path is implicitly '/')
288 hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);
289
290 String host = urlNoDots.substring(hostStart, hostEnd);
291 host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));
292
293 return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
294 } else {
295 return dots + url;
296 }
297 }
298
299 /**
300 * Get the file extension if it is on path as type "content://.../DocInfo.doc"
301 * @param filepath: Content Uri converted to string format
302 * @return String: fileExtension (type '.pdf'). Empty if no extension
303 */
304 public static String getComposedFileExtension(String filepath) {
305 String fileExtension = "";
306 String fileNameInContentUri = filepath.substring(filepath.lastIndexOf("/"));
307
308 // Check if extension is included in uri
309 int pos = fileNameInContentUri.lastIndexOf('.');
310 if (pos >= 0) {
311 fileExtension = fileNameInContentUri.substring(pos);
312 }
313 return fileExtension;
314 }
315
316 @SuppressWarnings("deprecation")
317 public static CharSequence getRelativeDateTimeString (
318 Context c, long time, long minResolution, long transitionResolution, int flags
319 ){
320
321 CharSequence dateString = "";
322
323 // in Future
324 if (time > System.currentTimeMillis()){
325 return DisplayUtils.unixTimeToHumanReadable(time);
326 }
327 // < 60 seconds -> seconds ago
328 else if ((System.currentTimeMillis() - time) < 60 * 1000) {
329 return c.getString(R.string.file_list_seconds_ago);
330 } else {
331 // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)
332 if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB &&
333 (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000 ) {
334 Date date = new Date(time);
335 date.setHours(0);
336 date.setMinutes(0);
337 date.setSeconds(0);
338 dateString = DateUtils.getRelativeDateTimeString(
339 c, date.getTime(), minResolution, transitionResolution, flags
340 );
341 } else {
342 dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);
343 }
344 }
345
346 return dateString.toString().split(",")[0];
347 }
348
349 /**
350 * Update the passed path removing the last "/" if it is not the root folder
351 * @param path
352 */
353 public static String getPathWithoutLastSlash(String path) {
354
355 // Remove last slash from path
356 if (path.length() > 1 && path.charAt(path.length()-1) == OCFile.PATH_SEPARATOR.charAt(0)) {
357 path = path.substring(0, path.length()-1);
358 }
359 return path;
360 }
361
362
363 /**
364 * Gets the screen size in pixels in a backwards compatible way
365 *
366 * @param caller Activity calling; needed to get access to the {@link android.view.WindowManager}
367 * @return Size in pixels of the screen, or default {@link Point} if caller is null
368 */
369 public static Point getScreenSize(Activity caller) {
370 Point size = new Point();
371 if (caller != null) {
372 Display display = caller.getWindowManager().getDefaultDisplay();
373 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
374 display.getSize(size);
375 } else {
376 size.set(display.getWidth(), display.getHeight());
377 }
378 }
379 return size;
380 }
381
382 /**
383 * Determines if user set folder to grid or list view. If folder is not set itself,
384 * it finds a parent that is set (at least root is set).
385 * @param file
386 * @param storageManager
387 * @return
388 */
389 public static boolean isGridView(OCFile file, FileDataStorageManager storageManager){
390 if (file != null) {
391 OCFile fileToTest = file;
392 OCFile parentDir = null;
393 String parentPath = null;
394
395 SharedPreferences setting = MainApp.getAppContext().getSharedPreferences(
396 "viewMode", Context.MODE_PRIVATE);
397
398 if (setting.contains(fileToTest.getRemoteId())) {
399 return setting.getBoolean(fileToTest.getRemoteId(), false);
400 } else {
401 do {
402 if (fileToTest.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
403 parentPath = new File(fileToTest.getRemotePath()).getParent();
404 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
405 parentPath + OCFile.PATH_SEPARATOR;
406 parentDir = storageManager.getFileByPath(parentPath);
407 } else {
408 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
409 }
410
411 while (parentDir == null) {
412 parentPath = new File(parentPath).getParent();
413 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
414 parentPath + OCFile.PATH_SEPARATOR;
415 parentDir = storageManager.getFileByPath(parentPath);
416 }
417 fileToTest = parentDir;
418 } while (endWhile(parentDir, setting));
419 return setting.getBoolean(fileToTest.getRemoteId(), false);
420 }
421 } else {
422 return false;
423 }
424 }
425
426 private static boolean endWhile(OCFile parentDir, SharedPreferences setting) {
427 if (parentDir.getRemotePath().compareToIgnoreCase(OCFile.ROOT_PATH) == 0) {
428 return false;
429 } else {
430 return !setting.contains(parentDir.getRemoteId());
431 }
432 }
433
434 public static void setViewMode(OCFile file, boolean setGrid){
435 SharedPreferences setting = MainApp.getAppContext().getSharedPreferences(
436 "viewMode", Context.MODE_PRIVATE);
437
438 SharedPreferences.Editor editor = setting.edit();
439 editor.putBoolean(file.getRemoteId(), setGrid);
440 editor.commit();
441 }
442
443 /**
444 * sets the coloring of the given progress bar to color_accent.
445 *
446 * @param progressBar the progress bar to be colored
447 */
448 public static void colorPreLollipopHorizontalProgressBar(ProgressBar progressBar) {
449 if (progressBar != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
450 int color = progressBar.getResources().getColor(R.color.color_accent);
451 progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
452 progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
453 }
454 }
455
456 /**
457 * sets the coloring of the given seek bar to color_accent.
458 *
459 * @param seekBar the seek bar to be colored
460 */
461 public static void colorPreLollipopHorizontalSeekBar(SeekBar seekBar) {
462 if (seekBar != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
463 colorPreLollipopHorizontalProgressBar(seekBar);
464
465 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
466 int color = seekBar.getResources().getColor(R.color.color_accent);
467 seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
468 seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
469 }
470 }
471 }
472 }