Merge pull request #740 from owncloud/revert-669-master
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / DisplayUtils.java
index 40bf119..da81f53 100644 (file)
@@ -18,6 +18,7 @@
 \r
 package com.owncloud.android.utils;\r
 \r
 \r
 package com.owncloud.android.utils;\r
 \r
+import java.net.IDN;\r
 import java.util.Arrays;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
 import java.util.Arrays;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
@@ -25,8 +26,13 @@ import java.util.HashMap;
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
-import android.util.Log;\r
+import android.annotation.TargetApi;\r
+import android.content.Context;\r
+import android.os.Build;\r
+import android.text.format.DateFormat;\r
+import android.text.format.DateUtils;\r
 \r
 \r
+import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
 \r
 /**\r
 import com.owncloud.android.R;\r
 \r
 /**\r
@@ -37,6 +43,8 @@ import com.owncloud.android.R;
  */\r
 public class DisplayUtils {\r
     \r
  */\r
 public class DisplayUtils {\r
     \r
+    private static final String OWNCLOUD_APP_NAME = "ownCloud";\r
+\r
     //private static String TAG = DisplayUtils.class.getSimpleName(); \r
     \r
     private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
     //private static String TAG = DisplayUtils.class.getSimpleName(); \r
     \r
     private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };\r
@@ -151,6 +159,7 @@ public class DisplayUtils {
      * known MIME type.\r
      * \r
      * @param mimetype      MIME type string.\r
      * known MIME type.\r
      * \r
      * @param mimetype      MIME type string.\r
+     * @param filename      name, with extension\r
      * @return              Resource identifier of an image resource.\r
      */\r
     public static int getResourceId(String mimetype, String filename) {\r
      * @return              Resource identifier of an image resource.\r
      */\r
     public static int getResourceId(String mimetype, String filename) {\r
@@ -230,10 +239,85 @@ public class DisplayUtils {
     \r
     \r
     public static int getSeasonalIconId() {\r
     \r
     \r
     public static int getSeasonalIconId() {\r
-        if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354) {\r
+        if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354 &&\r
+                MainApp.getAppContext().getString(R.string.app_name).equals(OWNCLOUD_APP_NAME)) {\r
             return R.drawable.winter_holidays_icon;\r
         } else {\r
             return R.drawable.icon;\r
         }\r
     }\r
             return R.drawable.winter_holidays_icon;\r
         } else {\r
             return R.drawable.icon;\r
         }\r
     }\r
+    \r
+    /**\r
+     * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.\r
+     * @param url the URL where the domain name should be converted\r
+     * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode\r
+     * @return the URL containing the converted domain name\r
+     */\r
+    @TargetApi(Build.VERSION_CODES.GINGERBREAD)\r
+    public static String convertIdn(String url, boolean toASCII) {\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
+                hostStart = url.indexOf("//") + "//".length();\r
+            } else if (url.indexOf("@") != -1) {\r
+                hostStart = url.indexOf("@") + "@".length();\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
+            host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));\r
+            \r
+            return url.substring(0, hostStart) + host + url.substring(hostEnd);\r
+        } else {\r
+            return url;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Get the file extension if it is on path as type "content://.../DocInfo.doc"\r
+     * @param filepath: Content Uri converted to string format\r
+     * @return String: fileExtension (type '.pdf'). Empty if no extension\r
+     */\r
+    public static String getComposedFileExtension(String filepath) {\r
+        String fileExtension = "";\r
+        String fileNameInContentUri = filepath.substring(filepath.lastIndexOf("/"));\r
+\r
+        // Check if extension is included in uri\r
+        int pos = fileNameInContentUri.lastIndexOf('.');\r
+        if (pos >= 0) {\r
+            fileExtension = fileNameInContentUri.substring(pos);\r
+        }\r
+        return fileExtension;\r
+    }\r
+\r
+    public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution, long transitionResolution, int flags){\r
+        CharSequence dateString = "";\r
+        \r
+        // in Future\r
+        if (time > System.currentTimeMillis()){\r
+            return DisplayUtils.unixTimeToHumanReadable(time);\r
+        } \r
+        // < 60 seconds -> seconds ago\r
+        else if ((System.currentTimeMillis() - time) < 60 * 1000) {\r
+            return c.getString(R.string.file_list_seconds_ago);\r
+        } else {\r
+            // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)\r
+            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB && (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000){\r
+                Date date = new Date(time);\r
+                date.setHours(0);\r
+                date.setMinutes(0);\r
+                date.setSeconds(0);\r
+                dateString = DateUtils.getRelativeDateTimeString(c, date.getTime(), minResolution, transitionResolution, flags);\r
+            } else {\r
+                dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);\r
+            }\r
+        }\r
+        \r
+        return dateString.toString().split(",")[0];
+    }\r
 }\r
 }\r