X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/99d7e189168fb85dda899d218ecfb3bd03a7f478..f15c2f32ceea2f2e6abff36f828a37196db3040a:/src/com/owncloud/android/utils/DisplayUtils.java diff --git a/src/com/owncloud/android/utils/DisplayUtils.java b/src/com/owncloud/android/utils/DisplayUtils.java index 8a281e92..25cc55a4 100644 --- a/src/com/owncloud/android/utils/DisplayUtils.java +++ b/src/com/owncloud/android/utils/DisplayUtils.java @@ -22,9 +22,21 @@ package com.owncloud.android.utils; +import java.io.File; +import java.net.IDN; +import java.text.DateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.Vector; + import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Point; import android.graphics.PorterDuff; import android.os.Build; @@ -35,8 +47,10 @@ import android.widget.SeekBar; import com.owncloud.android.MainApp; import com.owncloud.android.R; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import java.math.BigDecimal; import java.net.IDN; import java.text.DateFormat; import java.util.Calendar; @@ -52,6 +66,7 @@ public class DisplayUtils { private static final String OWNCLOUD_APP_NAME = "ownCloud"; private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + private static final int[] sizeScales = { 0, 0, 0, 1, 1, 2, 2, 2, 2 }; private static Map mimeType2HumanReadable; @@ -72,19 +87,24 @@ public class DisplayUtils { /** * Converts the file size in bytes to human readable output. - * + * + * * @param bytes Input file size * @return Like something readable like "12 MB" */ public static String bytesToHumanReadable(long bytes) { double result = bytes; - int attachedsuff = 0; - while (result > 1024 && attachedsuff < sizeSuffixes.length) { + int attachedSuff = 0; + while (result > 1024 && attachedSuff < sizeSuffixes.length) { result /= 1024.; - attachedsuff++; + attachedSuff++; } - result = ((int) (result * 100)) / 100.; - return result + " " + sizeSuffixes[attachedsuff]; + + return new BigDecimal(result).setScale( + sizeScales[attachedSuff], BigDecimal.ROUND_HALF_UP) + " " + sizeSuffixes[attachedSuff]; } /** @@ -193,22 +213,19 @@ public class DisplayUtils { else if ((System.currentTimeMillis() - time) < 60 * 1000) { return c.getString(R.string.file_list_seconds_ago); } else { - // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716) - if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB && - (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000 ) { - Date date = new Date(time); - date.setHours(0); - date.setMinutes(0); - date.setSeconds(0); - dateString = DateUtils.getRelativeDateTimeString( - c, date.getTime(), minResolution, transitionResolution, flags - ); - } else { - dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags); + dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags); + } + + String[] parts = dateString.toString().split(","); + if (parts.length == 2) { + if (parts[1].contains(":") && !parts[0].contains(":")) { + return parts[0]; + } else if (parts[0].contains(":") && !parts[1].contains(":")) { + return parts[1]; } } - - return dateString.toString().split(",")[0]; + //dateString contains unexpected format. fallback: use relative date time string from android api as is. + return dateString.toString(); } /** @@ -245,6 +262,67 @@ public class DisplayUtils { } /** + * Determines if user set folder to grid or list view. If folder is not set itself, + * it finds a parent that is set (at least root is set). + * @param file + * @param storageManager + * @return + */ + public static boolean isGridView(OCFile file, FileDataStorageManager storageManager){ + if (file != null) { + OCFile fileToTest = file; + OCFile parentDir = null; + String parentPath = null; + + SharedPreferences setting = MainApp.getAppContext().getSharedPreferences( + "viewMode", Context.MODE_PRIVATE); + + if (setting.contains(fileToTest.getRemoteId())) { + return setting.getBoolean(fileToTest.getRemoteId(), false); + } else { + do { + if (fileToTest.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) { + parentPath = new File(fileToTest.getRemotePath()).getParent(); + parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : + parentPath + OCFile.PATH_SEPARATOR; + parentDir = storageManager.getFileByPath(parentPath); + } else { + parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH); + } + + while (parentDir == null) { + parentPath = new File(parentPath).getParent(); + parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : + parentPath + OCFile.PATH_SEPARATOR; + parentDir = storageManager.getFileByPath(parentPath); + } + fileToTest = parentDir; + } while (endWhile(parentDir, setting)); + return setting.getBoolean(fileToTest.getRemoteId(), false); + } + } else { + return false; + } + } + + private static boolean endWhile(OCFile parentDir, SharedPreferences setting) { + if (parentDir.getRemotePath().compareToIgnoreCase(OCFile.ROOT_PATH) == 0) { + return false; + } else { + return !setting.contains(parentDir.getRemoteId()); + } + } + + public static void setViewMode(OCFile file, boolean setGrid){ + SharedPreferences setting = MainApp.getAppContext().getSharedPreferences( + "viewMode", Context.MODE_PRIVATE); + + SharedPreferences.Editor editor = setting.edit(); + editor.putBoolean(file.getRemoteId(), setGrid); + editor.commit(); + } + + /** * sets the coloring of the given progress bar to color_accent. * * @param progressBar the progress bar to be colored