2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
23 package com
.owncloud
.android
.utils
;
25 import android
.annotation
.TargetApi
;
26 import android
.app
.Activity
;
27 import android
.content
.Context
;
28 import android
.graphics
.Point
;
29 import android
.graphics
.PorterDuff
;
30 import android
.os
.Build
;
31 import android
.text
.format
.DateUtils
;
32 import android
.view
.Display
;
33 import android
.widget
.ProgressBar
;
34 import android
.widget
.SeekBar
;
36 import com
.owncloud
.android
.MainApp
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import java
.text
.DateFormat
;
42 import java
.util
.Calendar
;
43 import java
.util
.Date
;
44 import java
.util
.HashMap
;
48 * A helper class for some string operations.
50 public class DisplayUtils
{
52 private static final String OWNCLOUD_APP_NAME
= "ownCloud";
54 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
56 private static Map
<String
, String
> mimeType2HumanReadable
;
59 mimeType2HumanReadable
= new HashMap
<String
, String
>();
61 mimeType2HumanReadable
.put("image/jpeg", "JPEG image");
62 mimeType2HumanReadable
.put("image/jpg", "JPEG image");
63 mimeType2HumanReadable
.put("image/png", "PNG image");
64 mimeType2HumanReadable
.put("image/bmp", "Bitmap image");
65 mimeType2HumanReadable
.put("image/gif", "GIF image");
66 mimeType2HumanReadable
.put("image/svg+xml", "JPEG image");
67 mimeType2HumanReadable
.put("image/tiff", "TIFF image");
69 mimeType2HumanReadable
.put("audio/mpeg", "MP3 music file");
70 mimeType2HumanReadable
.put("application/ogg", "OGG music file");
74 * Converts the file size in bytes to human readable output.
76 * @param bytes Input file size
77 * @return Like something readable like "12 MB"
79 public static String
bytesToHumanReadable(long bytes
) {
80 double result
= bytes
;
82 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
86 result
= ((int) (result
* 100)) / 100.;
87 return result
+ " " + sizeSuffixes
[attachedsuff
];
91 * Converts MIME types like "image/jpg" to more end user friendly output
94 * @param mimetype MIME type to convert
95 * @return A human friendly version of the MIME type
97 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
98 if (mimeType2HumanReadable
.containsKey(mimetype
)) {
99 return mimeType2HumanReadable
.get(mimetype
);
101 if (mimetype
.split("/").length
>= 2)
102 return mimetype
.split("/")[1].toUpperCase() + " file";
103 return "Unknown type";
107 * Converts Unix time to human readable format
108 * @param milliseconds that have passed since 01/01/1970
109 * @return The human readable time for the users locale
111 public static String
unixTimeToHumanReadable(long milliseconds
) {
112 Date date
= new Date(milliseconds
);
113 DateFormat df
= DateFormat
.getDateTimeInstance();
114 return df
.format(date
);
117 public static int getSeasonalIconId() {
118 if (Calendar
.getInstance().get(Calendar
.DAY_OF_YEAR
) >= 354 &&
119 MainApp
.getAppContext().getString(R
.string
.app_name
).equals(OWNCLOUD_APP_NAME
)) {
120 return R
.drawable
.winter_holidays_icon
;
122 return R
.drawable
.icon
;
127 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
128 * @param url the URL where the domain name should be converted
129 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
130 * @return the URL containing the converted domain name
132 @TargetApi(Build
.VERSION_CODES
.GINGERBREAD
)
133 public static String
convertIdn(String url
, boolean toASCII
) {
135 String urlNoDots
= url
;
137 while (urlNoDots
.startsWith(".")) {
138 urlNoDots
= url
.substring(1);
142 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.GINGERBREAD
) {
143 // Find host name after '//' or '@'
145 if (urlNoDots
.indexOf("//") != -1) {
146 hostStart
= url
.indexOf("//") + "//".length();
147 } else if (url
.indexOf("@") != -1) {
148 hostStart
= url
.indexOf("@") + "@".length();
151 int hostEnd
= url
.substring(hostStart
).indexOf("/");
152 // Handle URL which doesn't have a path (path is implicitly '/')
153 hostEnd
= (hostEnd
== -1 ? urlNoDots
.length() : hostStart
+ hostEnd
);
155 String host
= urlNoDots
.substring(hostStart
, hostEnd
);
156 host
= (toASCII ? IDN
.toASCII(host
) : IDN
.toUnicode(host
));
158 return dots
+ urlNoDots
.substring(0, hostStart
) + host
+ urlNoDots
.substring(hostEnd
);
165 * Get the file extension if it is on path as type "content://.../DocInfo.doc"
166 * @param filepath: Content Uri converted to string format
167 * @return String: fileExtension (type '.pdf'). Empty if no extension
169 public static String
getComposedFileExtension(String filepath
) {
170 String fileExtension
= "";
171 String fileNameInContentUri
= filepath
.substring(filepath
.lastIndexOf("/"));
173 // Check if extension is included in uri
174 int pos
= fileNameInContentUri
.lastIndexOf('.');
176 fileExtension
= fileNameInContentUri
.substring(pos
);
178 return fileExtension
;
181 @SuppressWarnings("deprecation")
182 public static CharSequence
getRelativeDateTimeString (
183 Context c
, long time
, long minResolution
, long transitionResolution
, int flags
186 CharSequence dateString
= "";
189 if (time
> System
.currentTimeMillis()){
190 return DisplayUtils
.unixTimeToHumanReadable(time
);
192 // < 60 seconds -> seconds ago
193 else if ((System
.currentTimeMillis() - time
) < 60 * 1000) {
194 return c
.getString(R
.string
.file_list_seconds_ago
);
196 dateString
= DateUtils
.getRelativeDateTimeString(c
, time
, minResolution
, transitionResolution
, flags
);
199 String
[] parts
= dateString
.toString().split(",");
200 if (parts
.length
== 2) {
201 if (parts
[1].contains(":") && !parts
[0].contains(":")) {
203 } else if (parts
[0].contains(":") && !parts
[1].contains(":")) {
207 //dateString contains unexpected format. use localized, absolute date.
208 return dateString
.toString();
212 * Update the passed path removing the last "/" if it is not the root folder
215 public static String
getPathWithoutLastSlash(String path
) {
217 // Remove last slash from path
218 if (path
.length() > 1 && path
.charAt(path
.length()-1) == OCFile
.PATH_SEPARATOR
.charAt(0)) {
219 path
= path
.substring(0, path
.length()-1);
226 * Gets the screen size in pixels in a backwards compatible way
228 * @param caller Activity calling; needed to get access to the {@link android.view.WindowManager}
229 * @return Size in pixels of the screen, or default {@link Point} if caller is null
231 public static Point
getScreenSize(Activity caller
) {
232 Point size
= new Point();
233 if (caller
!= null
) {
234 Display display
= caller
.getWindowManager().getDefaultDisplay();
235 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
236 display
.getSize(size
);
238 size
.set(display
.getWidth(), display
.getHeight());
245 * sets the coloring of the given progress bar to color_accent.
247 * @param progressBar the progress bar to be colored
249 public static void colorPreLollipopHorizontalProgressBar(ProgressBar progressBar
) {
250 if (progressBar
!= null
&& Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.LOLLIPOP
) {
251 int color
= progressBar
.getResources().getColor(R
.color
.color_accent
);
252 progressBar
.getIndeterminateDrawable().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
253 progressBar
.getProgressDrawable().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
258 * sets the coloring of the given seek bar to color_accent.
260 * @param seekBar the seek bar to be colored
262 public static void colorPreLollipopHorizontalSeekBar(SeekBar seekBar
) {
263 if (seekBar
!= null
&& Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.LOLLIPOP
) {
264 colorPreLollipopHorizontalProgressBar(seekBar
);
266 if(Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
) {
267 int color
= seekBar
.getResources().getColor(R
.color
.color_accent
);
268 seekBar
.getThumb().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
269 seekBar
.getThumb().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);