Merge branch 'develop' of github.com:owncloud/android into switchListVsGrid
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / DisplayUtils.java
index 3ae36e4..b835f8b 100644 (file)
@@ -22,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
@@ -35,7 +36,7 @@ import java.util.Vector;
 import android.annotation.TargetApi;\r
 import android.app.Activity;\r
 import android.content.Context;\r
-import android.graphics.Color;\r
+import android.content.SharedPreferences;\r
 import android.graphics.Point;\r
 import android.graphics.PorterDuff;\r
 import android.os.Build;\r
@@ -47,6 +48,7 @@ import android.widget.SeekBar;
 \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
@@ -378,6 +380,67 @@ public class DisplayUtils {
     }\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
      * sets the coloring of the given progress bar to color_accent.\r
      *\r
      * @param progressBar the progress bar to be colored\r