Merge branch 'develop' of github.com:owncloud/android into switchListVsGrid
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / DisplayUtils.java
index 32d9017..1662a72 100644 (file)
@@ -1,6 +1,10 @@
-/* ownCloud Android client application\r
+/**\r
+ *   ownCloud Android client application\r
+ *\r
+ *   @author Bartek Przybylski\r
+ *   @author David A. Velasco\r
  *   Copyright (C) 2011  Bartek Przybylski\r
- *   Copyright (C) 2012-2013 ownCloud Inc.\r
+ *   Copyright (C) 2015 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License version 2,\r
@@ -18,6 +22,7 @@
 \r
 package com.owncloud.android.utils;\r
 \r
+import java.io.File;\r
 import java.net.IDN;\r
 import java.text.DateFormat;\r
 import java.util.Arrays;\r
@@ -26,22 +31,25 @@ import java.util.Date;
 import java.util.HashMap;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
+import java.util.Vector;\r
 \r
 import android.annotation.TargetApi;\r
+import android.app.Activity;\r
 import android.content.Context;\r
+import android.content.SharedPreferences;\r
+import android.graphics.Point;\r
 import android.os.Build;\r
 import android.text.format.DateUtils;\r
+import android.view.Display;\r
 import android.webkit.MimeTypeMap;\r
 \r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
+import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 \r
 /**\r
  * A helper class for some string operations.\r
- * \r
- * @author Bartek Przybylski\r
- * @author David A. Velasco\r
  */\r
 public class DisplayUtils {\r
     \r
@@ -228,7 +236,7 @@ public class DisplayUtils {
     \r
     /**\r
      * Converts Unix time to human readable format\r
-     * @param miliseconds that have passed since 01/01/1970\r
+     * @param milliseconds that have passed since 01/01/1970\r
      * @return The human readable time for the users locale\r
      */\r
     public static String unixTimeToHumanReadable(long milliseconds) {\r
@@ -255,26 +263,33 @@ public class DisplayUtils {
      */\r
     @TargetApi(Build.VERSION_CODES.GINGERBREAD)\r
     public static String convertIdn(String url, boolean toASCII) {\r
-        \r
+\r
+        String urlNoDots = url;\r
+        String dots="";\r
+        while (urlNoDots.startsWith(".")) {\r
+            urlNoDots = url.substring(1);\r
+            dots = dots + ".";\r
+        }\r
+\r
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {\r
             // Find host name after '//' or '@'\r
             int hostStart = 0;\r
-            if  (url.indexOf("//") != -1) {\r
+            if  (urlNoDots.indexOf("//") != -1) {\r
                 hostStart = url.indexOf("//") + "//".length();\r
             } else if (url.indexOf("@") != -1) {\r
                 hostStart = url.indexOf("@") + "@".length();\r
             }\r
-            \r
+\r
             int hostEnd = url.substring(hostStart).indexOf("/");\r
             // Handle URL which doesn't have a path (path is implicitly '/')\r
-            hostEnd = (hostEnd == -1 ? url.length() : hostStart + hostEnd);\r
-            \r
-            String host = url.substring(hostStart, hostEnd);\r
+            hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);\r
+\r
+            String host = urlNoDots.substring(hostStart, hostEnd);\r
             host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));\r
-            \r
-            return url.substring(0, hostStart) + host + url.substring(hostEnd);\r
+\r
+            return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);\r
         } else {\r
-            return url;\r
+            return dots + url;\r
         }\r
     }\r
 \r
@@ -340,4 +355,86 @@ public class DisplayUtils {
         }\r
         return path;\r
     }\r
+\r
+\r
+    /**\r
+     * Gets the screen size in pixels in a backwards compatible way\r
+     *\r
+     * @param caller        Activity calling; needed to get access to the {@link android.view.WindowManager}\r
+     * @return              Size in pixels of the screen, or default {@link Point} if caller is null\r
+     */\r
+    public static Point getScreenSize(Activity caller) {\r
+        Point size = new Point();\r
+        if (caller != null) {\r
+            Display display = caller.getWindowManager().getDefaultDisplay();\r
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {\r
+                display.getSize(size);\r
+            } else {\r
+                size.set(display.getWidth(), display.getHeight());\r
+            }\r
+        }\r
+        return size;\r
+    }\r
+\r
+    /**\r
+     * Determines if user set folder to grid or list view. If folder is not set itself,\r
+     * it finds a parent that is set (at least root is set).\r
+     * @param file\r
+     * @param storageManager\r
+     * @return\r
+     */\r
+    public static boolean isGridView(OCFile file, FileDataStorageManager storageManager){\r
+        if (file != null) {\r
+            OCFile fileToTest = file;\r
+            OCFile parentDir = null;\r
+            String parentPath = null;\r
+\r
+            SharedPreferences setting = MainApp.getAppContext().getSharedPreferences(\r
+                    "viewMode", Context.MODE_PRIVATE);\r
+\r
+            if (setting.contains(fileToTest.getRemoteId())) {\r
+                return setting.getBoolean(fileToTest.getRemoteId(), false);\r
+            } else {\r
+                do {\r
+                    if (fileToTest.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {\r
+                        parentPath = new File(fileToTest.getRemotePath()).getParent();\r
+                        parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :\r
+                                parentPath + OCFile.PATH_SEPARATOR;\r
+                        parentDir = storageManager.getFileByPath(parentPath);\r
+                    } else {\r
+                        parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);\r
+                    }\r
+\r
+                    while (parentDir == null) {\r
+                        parentPath = new File(parentPath).getParent();\r
+                        parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :\r
+                                parentPath + OCFile.PATH_SEPARATOR;\r
+                        parentDir = storageManager.getFileByPath(parentPath);\r
+                    }\r
+                    fileToTest = parentDir;\r
+                } while (endWhile(parentDir, setting));\r
+                return setting.getBoolean(fileToTest.getRemoteId(), false);\r
+            }\r
+        } else {\r
+            return false;\r
+        }\r
+    }\r
+\r
+    private static boolean endWhile(OCFile parentDir, SharedPreferences setting) {\r
+        if (parentDir.getRemotePath().compareToIgnoreCase(OCFile.ROOT_PATH) == 0) {\r
+            return false;\r
+        } else {\r
+            return !setting.contains(parentDir.getRemoteId());\r
+        }\r
+    }\r
+\r
+    public static void setViewMode(OCFile file, boolean setGrid){\r
+        SharedPreferences setting = MainApp.getAppContext().getSharedPreferences(\r
+                "viewMode", Context.MODE_PRIVATE);\r
+\r
+        SharedPreferences.Editor editor = setting.edit();\r
+        editor.putBoolean(file.getRemoteId(), setGrid);\r
+        editor.commit();\r
+    }\r
+\r
 }\r