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
;
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
;
34 import java
.util
.Vector
;
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
.widget
.ProgressBar
;
46 import android
.widget
.SeekBar
;
48 import com
.owncloud
.android
.MainApp
;
49 import com
.owncloud
.android
.R
;
50 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
51 import com
.owncloud
.android
.datamodel
.OCFile
;
53 import java
.math
.BigDecimal
;
55 import java
.text
.DateFormat
;
56 import java
.util
.Calendar
;
57 import java
.util
.Date
;
58 import java
.util
.HashMap
;
62 * A helper class for some string operations.
64 public class DisplayUtils
{
66 private static final String OWNCLOUD_APP_NAME
= "ownCloud";
68 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
69 private static final int[] sizeScales
= { 0, 0, 0, 1, 1, 2, 2, 2, 2 };
71 private static Map
<String
, String
> mimeType2HumanReadable
;
74 mimeType2HumanReadable
= new HashMap
<String
, String
>();
76 mimeType2HumanReadable
.put("image/jpeg", "JPEG image");
77 mimeType2HumanReadable
.put("image/jpg", "JPEG image");
78 mimeType2HumanReadable
.put("image/png", "PNG image");
79 mimeType2HumanReadable
.put("image/bmp", "Bitmap image");
80 mimeType2HumanReadable
.put("image/gif", "GIF image");
81 mimeType2HumanReadable
.put("image/svg+xml", "JPEG image");
82 mimeType2HumanReadable
.put("image/tiff", "TIFF image");
84 mimeType2HumanReadable
.put("audio/mpeg", "MP3 music file");
85 mimeType2HumanReadable
.put("application/ogg", "OGG music file");
89 * Converts the file size in bytes to human readable output.
91 * <li>appends a size suffix, e.g. B, KB, MB etc.</li>
92 * <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>
95 * @param bytes Input file size
96 * @return Like something readable like "12 MB"
98 public static String
bytesToHumanReadable(long bytes
) {
99 double result
= bytes
;
100 int attachedSuff
= 0;
101 while (result
> 1024 && attachedSuff
< sizeSuffixes
.length
) {
106 return new BigDecimal(result
).setScale(
107 sizeScales
[attachedSuff
], BigDecimal
.ROUND_HALF_UP
) + " " + sizeSuffixes
[attachedSuff
];
111 * Converts MIME types like "image/jpg" to more end user friendly output
114 * @param mimetype MIME type to convert
115 * @return A human friendly version of the MIME type
117 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
118 if (mimeType2HumanReadable
.containsKey(mimetype
)) {
119 return mimeType2HumanReadable
.get(mimetype
);
121 if (mimetype
.split("/").length
>= 2)
122 return mimetype
.split("/")[1].toUpperCase() + " file";
123 return "Unknown type";
127 * Converts Unix time to human readable format
128 * @param milliseconds that have passed since 01/01/1970
129 * @return The human readable time for the users locale
131 public static String
unixTimeToHumanReadable(long milliseconds
) {
132 Date date
= new Date(milliseconds
);
133 DateFormat df
= DateFormat
.getDateTimeInstance();
134 return df
.format(date
);
137 public static int getSeasonalIconId() {
138 if (Calendar
.getInstance().get(Calendar
.DAY_OF_YEAR
) >= 354 &&
139 MainApp
.getAppContext().getString(R
.string
.app_name
).equals(OWNCLOUD_APP_NAME
)) {
140 return R
.drawable
.winter_holidays_icon
;
142 return R
.drawable
.icon
;
147 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
148 * @param url the URL where the domain name should be converted
149 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
150 * @return the URL containing the converted domain name
152 @TargetApi(Build
.VERSION_CODES
.GINGERBREAD
)
153 public static String
convertIdn(String url
, boolean toASCII
) {
155 String urlNoDots
= url
;
157 while (urlNoDots
.startsWith(".")) {
158 urlNoDots
= url
.substring(1);
162 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.GINGERBREAD
) {
163 // Find host name after '//' or '@'
165 if (urlNoDots
.indexOf("//") != -1) {
166 hostStart
= url
.indexOf("//") + "//".length();
167 } else if (url
.indexOf("@") != -1) {
168 hostStart
= url
.indexOf("@") + "@".length();
171 int hostEnd
= url
.substring(hostStart
).indexOf("/");
172 // Handle URL which doesn't have a path (path is implicitly '/')
173 hostEnd
= (hostEnd
== -1 ? urlNoDots
.length() : hostStart
+ hostEnd
);
175 String host
= urlNoDots
.substring(hostStart
, hostEnd
);
176 host
= (toASCII ? IDN
.toASCII(host
) : IDN
.toUnicode(host
));
178 return dots
+ urlNoDots
.substring(0, hostStart
) + host
+ urlNoDots
.substring(hostEnd
);
185 * Get the file extension if it is on path as type "content://.../DocInfo.doc"
186 * @param filepath: Content Uri converted to string format
187 * @return String: fileExtension (type '.pdf'). Empty if no extension
189 public static String
getComposedFileExtension(String filepath
) {
190 String fileExtension
= "";
191 String fileNameInContentUri
= filepath
.substring(filepath
.lastIndexOf("/"));
193 // Check if extension is included in uri
194 int pos
= fileNameInContentUri
.lastIndexOf('.');
196 fileExtension
= fileNameInContentUri
.substring(pos
);
198 return fileExtension
;
201 @SuppressWarnings("deprecation")
202 public static CharSequence
getRelativeDateTimeString (
203 Context c
, long time
, long minResolution
, long transitionResolution
, int flags
206 CharSequence dateString
= "";
209 if (time
> System
.currentTimeMillis()){
210 return DisplayUtils
.unixTimeToHumanReadable(time
);
212 // < 60 seconds -> seconds ago
213 else if ((System
.currentTimeMillis() - time
) < 60 * 1000) {
214 return c
.getString(R
.string
.file_list_seconds_ago
);
216 // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)
217 if ( Build
.VERSION
.SDK_INT
<= Build
.VERSION_CODES
.HONEYCOMB
&&
218 (System
.currentTimeMillis() - time
) > 24 * 60 * 60 * 1000 ) {
219 Date date
= new Date(time
);
223 dateString
= DateUtils
.getRelativeDateTimeString(
224 c
, date
.getTime(), minResolution
, transitionResolution
, flags
227 dateString
= DateUtils
.getRelativeDateTimeString(c
, time
, minResolution
, transitionResolution
, flags
);
231 String
[] parts
= dateString
.toString().split(",");
232 if (parts
.length
== 2) {
233 if (parts
[1].contains(":") && !parts
[0].contains(":")) {
235 } else if (parts
[0].contains(":") && !parts
[1].contains(":")) {
239 //dateString contains unexpected format. use localized, absolute date.
240 return DisplayUtils
.unixTimeToHumanReadable(time
);
244 * Update the passed path removing the last "/" if it is not the root folder
247 public static String
getPathWithoutLastSlash(String path
) {
249 // Remove last slash from path
250 if (path
.length() > 1 && path
.charAt(path
.length()-1) == OCFile
.PATH_SEPARATOR
.charAt(0)) {
251 path
= path
.substring(0, path
.length()-1);
258 * Gets the screen size in pixels in a backwards compatible way
260 * @param caller Activity calling; needed to get access to the {@link android.view.WindowManager}
261 * @return Size in pixels of the screen, or default {@link Point} if caller is null
263 public static Point
getScreenSize(Activity caller
) {
264 Point size
= new Point();
265 if (caller
!= null
) {
266 Display display
= caller
.getWindowManager().getDefaultDisplay();
267 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
268 display
.getSize(size
);
270 size
.set(display
.getWidth(), display
.getHeight());
277 * Determines if user set folder to grid or list view. If folder is not set itself,
278 * it finds a parent that is set (at least root is set).
280 * @param storageManager
283 public static boolean isGridView(OCFile file
, FileDataStorageManager storageManager
){
285 OCFile fileToTest
= file
;
286 OCFile parentDir
= null
;
287 String parentPath
= null
;
289 SharedPreferences setting
= MainApp
.getAppContext().getSharedPreferences(
290 "viewMode", Context
.MODE_PRIVATE
);
292 if (setting
.contains(fileToTest
.getRemoteId())) {
293 return setting
.getBoolean(fileToTest
.getRemoteId(), false
);
296 if (fileToTest
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
297 parentPath
= new File(fileToTest
.getRemotePath()).getParent();
298 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
299 parentPath
+ OCFile
.PATH_SEPARATOR
;
300 parentDir
= storageManager
.getFileByPath(parentPath
);
302 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
305 while (parentDir
== null
) {
306 parentPath
= new File(parentPath
).getParent();
307 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
308 parentPath
+ OCFile
.PATH_SEPARATOR
;
309 parentDir
= storageManager
.getFileByPath(parentPath
);
311 fileToTest
= parentDir
;
312 } while (endWhile(parentDir
, setting
));
313 return setting
.getBoolean(fileToTest
.getRemoteId(), false
);
320 private static boolean endWhile(OCFile parentDir
, SharedPreferences setting
) {
321 if (parentDir
.getRemotePath().compareToIgnoreCase(OCFile
.ROOT_PATH
) == 0) {
324 return !setting
.contains(parentDir
.getRemoteId());
328 public static void setViewMode(OCFile file
, boolean setGrid
){
329 SharedPreferences setting
= MainApp
.getAppContext().getSharedPreferences(
330 "viewMode", Context
.MODE_PRIVATE
);
332 SharedPreferences
.Editor editor
= setting
.edit();
333 editor
.putBoolean(file
.getRemoteId(), setGrid
);
338 * sets the coloring of the given progress bar to color_accent.
340 * @param progressBar the progress bar to be colored
342 public static void colorPreLollipopHorizontalProgressBar(ProgressBar progressBar
) {
343 if (progressBar
!= null
&& Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.LOLLIPOP
) {
344 int color
= progressBar
.getResources().getColor(R
.color
.color_accent
);
345 progressBar
.getIndeterminateDrawable().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
346 progressBar
.getProgressDrawable().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
351 * sets the coloring of the given seek bar to color_accent.
353 * @param seekBar the seek bar to be colored
355 public static void colorPreLollipopHorizontalSeekBar(SeekBar seekBar
) {
356 if (seekBar
!= null
&& Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.LOLLIPOP
) {
357 colorPreLollipopHorizontalProgressBar(seekBar
);
359 if(Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN
) {
360 int color
= seekBar
.getResources().getColor(R
.color
.color_accent
);
361 seekBar
.getThumb().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);
362 seekBar
.getThumb().setColorFilter(color
, PorterDuff
.Mode
.SRC_IN
);