Merge branch 'new_filetype_icons' of https://github.com/owncloud/android into materia...
[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 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;
35
36 import com.owncloud.android.MainApp;
37 import com.owncloud.android.R;
38 import com.owncloud.android.datamodel.OCFile;
39
40 import java.math.BigDecimal;
41 import java.net.IDN;
42 import java.text.DateFormat;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.HashMap;
46 import java.util.Map;
47
48 /**
49 * A helper class for some string operations.
50 */
51 public class DisplayUtils {
52
53 private static final String OWNCLOUD_APP_NAME = "ownCloud";
54
55 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
56 private static final int[] sizeScales = { 0, 0, 0, 1, 1, 2, 2, 2, 2 };
57
58 private static Map<String, String> mimeType2HumanReadable;
59
60 static {
61 mimeType2HumanReadable = new HashMap<String, String>();
62 // images
63 mimeType2HumanReadable.put("image/jpeg", "JPEG image");
64 mimeType2HumanReadable.put("image/jpg", "JPEG image");
65 mimeType2HumanReadable.put("image/png", "PNG image");
66 mimeType2HumanReadable.put("image/bmp", "Bitmap image");
67 mimeType2HumanReadable.put("image/gif", "GIF image");
68 mimeType2HumanReadable.put("image/svg+xml", "JPEG image");
69 mimeType2HumanReadable.put("image/tiff", "TIFF image");
70 // music
71 mimeType2HumanReadable.put("audio/mpeg", "MP3 music file");
72 mimeType2HumanReadable.put("application/ogg", "OGG music file");
73 }
74
75 /**
76 * Converts the file size in bytes to human readable output.
77 * <ul>
78 * <li>appends a size suffix, e.g. B, KB, MB etc.</li>
79 * <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>
80 * </ul>
81 *
82 * @param bytes Input file size
83 * @return Like something readable like "12 MB"
84 */
85 public static String bytesToHumanReadable(long bytes) {
86 double result = bytes;
87 int attachedSuff = 0;
88 while (result > 1024 && attachedSuff < sizeSuffixes.length) {
89 result /= 1024.;
90 attachedSuff++;
91 }
92
93 return new BigDecimal(result).setScale(
94 sizeScales[attachedSuff], BigDecimal.ROUND_HALF_UP) + " " + sizeSuffixes[attachedSuff];
95 }
96
97 /**
98 * Converts MIME types like "image/jpg" to more end user friendly output
99 * like "JPG image".
100 *
101 * @param mimetype MIME type to convert
102 * @return A human friendly version of the MIME type
103 */
104 public static String convertMIMEtoPrettyPrint(String mimetype) {
105 if (mimeType2HumanReadable.containsKey(mimetype)) {
106 return mimeType2HumanReadable.get(mimetype);
107 }
108 if (mimetype.split("/").length >= 2)
109 return mimetype.split("/")[1].toUpperCase() + " file";
110 return "Unknown type";
111 }
112
113 /**
114 * Converts Unix time to human readable format
115 * @param milliseconds that have passed since 01/01/1970
116 * @return The human readable time for the users locale
117 */
118 public static String unixTimeToHumanReadable(long milliseconds) {
119 Date date = new Date(milliseconds);
120 DateFormat df = DateFormat.getDateTimeInstance();
121 return df.format(date);
122 }
123
124 public static int getSeasonalIconId() {
125 if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354 &&
126 MainApp.getAppContext().getString(R.string.app_name).equals(OWNCLOUD_APP_NAME)) {
127 return R.drawable.winter_holidays_icon;
128 } else {
129 return R.drawable.icon;
130 }
131 }
132
133 /**
134 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
135 * @param url the URL where the domain name should be converted
136 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
137 * @return the URL containing the converted domain name
138 */
139 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
140 public static String convertIdn(String url, boolean toASCII) {
141
142 String urlNoDots = url;
143 String dots="";
144 while (urlNoDots.startsWith(".")) {
145 urlNoDots = url.substring(1);
146 dots = dots + ".";
147 }
148
149 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
150 // Find host name after '//' or '@'
151 int hostStart = 0;
152 if (urlNoDots.indexOf("//") != -1) {
153 hostStart = url.indexOf("//") + "//".length();
154 } else if (url.indexOf("@") != -1) {
155 hostStart = url.indexOf("@") + "@".length();
156 }
157
158 int hostEnd = url.substring(hostStart).indexOf("/");
159 // Handle URL which doesn't have a path (path is implicitly '/')
160 hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);
161
162 String host = urlNoDots.substring(hostStart, hostEnd);
163 host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));
164
165 return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
166 } else {
167 return dots + url;
168 }
169 }
170
171 /**
172 * Get the file extension if it is on path as type "content://.../DocInfo.doc"
173 * @param filepath: Content Uri converted to string format
174 * @return String: fileExtension (type '.pdf'). Empty if no extension
175 */
176 public static String getComposedFileExtension(String filepath) {
177 String fileExtension = "";
178 String fileNameInContentUri = filepath.substring(filepath.lastIndexOf("/"));
179
180 // Check if extension is included in uri
181 int pos = fileNameInContentUri.lastIndexOf('.');
182 if (pos >= 0) {
183 fileExtension = fileNameInContentUri.substring(pos);
184 }
185 return fileExtension;
186 }
187
188 @SuppressWarnings("deprecation")
189 public static CharSequence getRelativeDateTimeString (
190 Context c, long time, long minResolution, long transitionResolution, int flags
191 ){
192
193 CharSequence dateString = "";
194
195 // in Future
196 if (time > System.currentTimeMillis()){
197 return DisplayUtils.unixTimeToHumanReadable(time);
198 }
199 // < 60 seconds -> seconds ago
200 else if ((System.currentTimeMillis() - time) < 60 * 1000) {
201 return c.getString(R.string.file_list_seconds_ago);
202 } else {
203 // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)
204 if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB &&
205 (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000 ) {
206 Date date = new Date(time);
207 date.setHours(0);
208 date.setMinutes(0);
209 date.setSeconds(0);
210 dateString = DateUtils.getRelativeDateTimeString(
211 c, date.getTime(), minResolution, transitionResolution, flags
212 );
213 } else {
214 dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);
215 }
216 }
217
218 return dateString.toString().split(",")[0];
219 }
220
221 /**
222 * Update the passed path removing the last "/" if it is not the root folder
223 * @param path
224 */
225 public static String getPathWithoutLastSlash(String path) {
226
227 // Remove last slash from path
228 if (path.length() > 1 && path.charAt(path.length()-1) == OCFile.PATH_SEPARATOR.charAt(0)) {
229 path = path.substring(0, path.length()-1);
230 }
231 return path;
232 }
233
234
235 /**
236 * Gets the screen size in pixels in a backwards compatible way
237 *
238 * @param caller Activity calling; needed to get access to the {@link android.view.WindowManager}
239 * @return Size in pixels of the screen, or default {@link Point} if caller is null
240 */
241 public static Point getScreenSize(Activity caller) {
242 Point size = new Point();
243 if (caller != null) {
244 Display display = caller.getWindowManager().getDefaultDisplay();
245 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
246 display.getSize(size);
247 } else {
248 size.set(display.getWidth(), display.getHeight());
249 }
250 }
251 return size;
252 }
253
254 /**
255 * sets the coloring of the given progress bar to color_accent.
256 *
257 * @param progressBar the progress bar to be colored
258 */
259 public static void colorPreLollipopHorizontalProgressBar(ProgressBar progressBar) {
260 if (progressBar != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
261 int color = progressBar.getResources().getColor(R.color.color_accent);
262 progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
263 progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
264 }
265 }
266
267 /**
268 * sets the coloring of the given seek bar to color_accent.
269 *
270 * @param seekBar the seek bar to be colored
271 */
272 public static void colorPreLollipopHorizontalSeekBar(SeekBar seekBar) {
273 if (seekBar != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
274 colorPreLollipopHorizontalProgressBar(seekBar);
275
276 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
277 int color = seekBar.getResources().getColor(R.color.color_accent);
278 seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
279 seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
280 }
281 }
282 }
283 }