Class AssetUtil


  • public class AssetUtil
    extends java.lang.Object
    A helper class to deal with assets, mainly to copy the assets to a place where the C++ core can reach it.
    • Constructor Summary

      Constructors 
      Constructor Description
      AssetUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean assetExists​(android.content.Context context, java.lang.String path)  
      static void copyAllAssetFiles​(android.content.Context context, java.lang.String... assetPaths)  
      static void copyAnylineAssets​(android.content.Context context, org.json.JSONObject json, java.lang.String mapJsonName, java.io.File dirToCopyTo, boolean isForceCopy)
      Copy the assets of the given map listed in the given json object (must be a map with filename relative to assets as the key and the md5 sum of the file as value) to the given directory.
      static void copyAssetFile​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, boolean isForceCopy)
      Copy the asset file specified by path to given directory.
      static void copyAssetFile​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, boolean isForceCopy, java.lang.String pathPrefix)  
      static void copyAssetFile​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, java.lang.String md5Hash)
      Copy the asset file specified by path to given directory.
      static void copyAssetFile​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, java.lang.String md5Hash, java.lang.String pathPrefix)  
      static void copyAssetFileWithoutPath​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, boolean isForceCopy)
      Copy the asset file specified by path directly to the given directory (without the relative path of the asset).
      static void copyAssetFileWithoutPath​(android.content.Context context, java.lang.String pathInAssets, java.io.File dirToCopyTo, java.lang.String md5Hash)
      Copy the asset file specified by path directly to the given directory (without the relative path of the asset).
      static void copyDirectory​(java.io.File sourceDir, java.io.File destDir)
      Copy a whole directory to another location.
      static boolean deleteDirectory​(java.io.File directoryToBeDeleted)
      Deletes a directory recursively
      static boolean fileExists​(java.lang.String path, java.lang.String fileName)  
      static org.json.JSONObject getAnylineAssetsJson​(android.content.Context context, java.lang.String fileName)
      Get the json object from the assets with the given file name.
      static java.lang.String getAssetPathForPlugin​(ScanController.PluginType pluginType)  
      static java.lang.String readFile​(java.lang.String path, java.lang.String fileName)
      Read the text of the given file within the assets.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AssetUtil

        public AssetUtil()
    • Method Detail

      • readFile

        public static java.lang.String readFile​(java.lang.String path,
                                                java.lang.String fileName)
                                         throws java.io.IOException
        Read the text of the given file within the assets.
        Parameters:
        fileName - the file name of the file to read (full path within the assets folder)
        Returns:
        the text of the file
        Throws:
        java.io.IOException - if the file could not be found or read
      • getAnylineAssetsJson

        public static org.json.JSONObject getAnylineAssetsJson​(android.content.Context context,
                                                               java.lang.String fileName)
                                                        throws java.io.IOException
        Get the json object from the assets with the given file name.
        Parameters:
        context - the context
        fileName - the name of the json file in the assets folder to read
        Returns:
        the json object from the assets with the given file name.
        Throws:
        java.io.IOException - if the file could not be found or it is not a valid json.
      • copyAnylineAssets

        public static void copyAnylineAssets​(android.content.Context context,
                                             org.json.JSONObject json,
                                             java.lang.String mapJsonName,
                                             java.io.File dirToCopyTo,
                                             boolean isForceCopy)
                                      throws org.json.JSONException,
                                             java.io.IOException
        Copy the assets of the given map listed in the given json object (must be a map with filename relative to assets as the key and the md5 sum of the file as value) to the given directory. The file will only be copied if it does not exist in the target location, or the saved md5 for that file is different to the one in the given json object, or if the isForcedCopy flag is true.
        Parameters:
        context - the context
        json - the json file containing a map with asset paths and md5 sums
        mapJsonName - the name of the map within the json object
        dirToCopyTo - the dir to copy the assets to
        isForceCopy - true if files should be copied regardless of the hashes
        Throws:
        org.json.JSONException - if there is a problem parsing the json (e.g. if the given map name does not exist)
        java.io.IOException - if there is any problem copying the files
      • copyAssetFile

        public static void copyAssetFile​(android.content.Context context,
                                         java.lang.String pathInAssets,
                                         java.io.File dirToCopyTo,
                                         boolean isForceCopy)
                                  throws java.io.IOException

        Copy the asset file specified by path to given directory.

        Md5 sum will be calculated and stored to the target dir with .md5 ending. Calculating this takes time, prefer auto generating the anyline_assets.json or providing the md5 with copyAssetFile(Context, String, File, String).

        If isForceCopy is false, this will not copy anything if the same .md5 file is in the target dir.

        Parameters:
        context - the context
        pathInAssets - Path to asset, relative to app's assets directory.
        dirToCopyTo - the target directory
        isForceCopy - if true the file will always be copied (an md5 will be calculated and checked otherwise)
        Throws:
        java.io.IOException - if there is any problem copying the files
      • copyAssetFile

        public static void copyAssetFile​(android.content.Context context,
                                         java.lang.String pathInAssets,
                                         java.io.File dirToCopyTo,
                                         boolean isForceCopy,
                                         java.lang.String pathPrefix)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • copyAllAssetFiles

        public static void copyAllAssetFiles​(android.content.Context context,
                                             java.lang.String... assetPaths)
                                      throws java.io.IOException,
                                             org.json.JSONException
        Throws:
        java.io.IOException
        org.json.JSONException
      • copyAssetFile

        public static void copyAssetFile​(android.content.Context context,
                                         java.lang.String pathInAssets,
                                         java.io.File dirToCopyTo,
                                         java.lang.String md5Hash)
                                  throws java.io.IOException

        Copy the asset file specified by path to given directory.

        The file will only be copied if it does not exist in the target location, or the saved md5 for that file is different to the one given. (The file will always be copied if the given hash is null)

        Parameters:
        context - the context
        pathInAssets - Path to asset, relative to app's assets directory.
        dirToCopyTo - the target directory
        md5Hash - the md5 checksum for the file to copy
        Throws:
        java.io.IOException - if there is any problem copying the files
      • copyAssetFile

        public static void copyAssetFile​(android.content.Context context,
                                         java.lang.String pathInAssets,
                                         java.io.File dirToCopyTo,
                                         java.lang.String md5Hash,
                                         java.lang.String pathPrefix)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • copyAssetFileWithoutPath

        public static void copyAssetFileWithoutPath​(android.content.Context context,
                                                    java.lang.String pathInAssets,
                                                    java.io.File dirToCopyTo,
                                                    boolean isForceCopy)
                                             throws java.io.IOException

        Copy the asset file specified by path directly to the given directory (without the relative path of the asset).

        Md5 sum will be calculated and stored to the target dir with .md5 ending. Calculating this takes time, prefer auto generating the anyline_assets.json or providing the md5 with copyAssetFileWithoutPath(Context, String, File, String).

        If isForceCopy is false, this will not copy anything if the same .md5 file is in the target dir.

        Parameters:
        context - the context
        pathInAssets - Path to asset, relative to app's assets directory.
        dirToCopyTo - the target directory
        isForceCopy - if true the file will always be copied (an md5 will be calculated and checked otherwise)
        Throws:
        java.io.IOException - if there is any problem copying the files
      • copyAssetFileWithoutPath

        public static void copyAssetFileWithoutPath​(android.content.Context context,
                                                    java.lang.String pathInAssets,
                                                    java.io.File dirToCopyTo,
                                                    java.lang.String md5Hash)
                                             throws java.io.IOException

        Copy the asset file specified by path directly to the given directory (without the relative path of the asset).

        The file will only be copied if it does not exist in the target location, or the saved md5 for that file is different to the one given. (The file will always be copied if the given hash is null)

        Parameters:
        context - the context
        pathInAssets - Path to asset, relative to app's assets directory.
        dirToCopyTo - the target directory
        md5Hash - the md5 checksum for the file to copy
        Throws:
        java.io.IOException - if there is any problem copying the files
      • copyDirectory

        public static void copyDirectory​(java.io.File sourceDir,
                                         java.io.File destDir)
                                  throws java.io.IOException
        Copy a whole directory to another location.
        Parameters:
        sourceDir - a File object represents the source directory
        destDir - a File object represents the destination directory
        Throws:
        java.io.IOException - thrown if IO error occurred.
      • deleteDirectory

        public static boolean deleteDirectory​(java.io.File directoryToBeDeleted)
        Deletes a directory recursively
        Parameters:
        directoryToBeDeleted - folder to be deleted recursively
        Returns:
        success of the operation
      • fileExists

        public static boolean fileExists​(java.lang.String path,
                                         java.lang.String fileName)
      • assetExists

        public static boolean assetExists​(android.content.Context context,
                                          java.lang.String path)
                                   throws java.io.IOException
        Throws:
        java.io.IOException