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