Using Resource Blocks and Resource Packs
You can code your resource handler to put resources into resource blocks and resource packs.
Resource blocks are a group of resources. For example, you might bundle splash screens into one block, images into another block, and audio files into another block.
Resource Packs are bundles of resource blocks.
The use of resource blocks and packs is not required. Reasons to use them might be to save space and/or increase speed. A single larger file compresses better than multiple smaller files. Opening one file can be faster than opening several smaller files. However, with current devices, space and speed are no longer major issues for mobile applications. As a result, using resource bundles and packs for reasons of space and speed is not typically needed.
Another reason you might want to use resource blocks and packs is for better security. When bundling resources, it is more difficult to determine names of resources and more difficult to take malicious actions without having to rebuild the resource packs.
Defining Resource Blocks
To define resource blocks in the resource handler code, execute the
startResourceBlock method that is in the
com.softwareag.mobile.reshandler.AntTaskResourceHandler class. For information about how to use an
AntTaskResourceHandler method in your resource handler, see
Methods that
Mobile Designer Provides for the Resource Handler.
When you execute the startResourceBlock method, you assign the resource block a name. You can assign resource blocks any name that is appropriate for your application. When you execute the startResourceBlock method, the resource block you create becomes the current resource block and all subsequent resources you add are included in the current block. For example, the following code sample shows how to start a resource block named "IMAGES" and add the file "logo.png" as the resource name "res_logo.png":
public class ResHandler extends com.softwareag.mobile.reshandler.ResourceHandler
{
public void projectResourceScript()
{
.
.
.
rh.startResourceBlock ("IMAGES");
rh.setResourceReadSubdirectory ("graphics"));
rh.addFile ("res_logo.png", "logo.png");
.
.
.
}
}
To start a new resource block, execute the startResourceBlock method again.
Using resource blocks helps you to control the memory your application uses. When you bundle resources into a block, you can cache the block(s) your application needs based on the application state. In other words, your application can load and unload blocks so that only the resources that are required for a state in your application are loaded and therefore the application is not using valuable memory space for unneeded resources.
Important: | If your resource handler bundles resources into blocks, when your application needs to use the resources that are in a block, be sure the application code caches the block into memory before loading its resources. |
A best practice is to use the same resource blocks for all the platforms for which you build your application. However, if the devices on which your application runs have varying memory and you are concerned that some devices cannot keep all the resources in memory, you might want to split your resources up in the application bundle for compression, decompression, or run-time memory management. One approach you can use is to bundle the blocks into packs.
Using Resource Packs
Resource packs are bundles of resource blocks. You can define several packs of different combinations of your resource bundles. You might customize packs for each of the devices your application supports. For example, some devices might have the memory capacity to cache all the blocks in memory at application load time, while other devices might only be able to cache one block at a time due to memory limitations.
To define resource packs in the resource handler code, execute the allocateResourceBlockToPack method that is in the AntTaskResourceHandler class. When you execute the allocateResourceBlockToPack method, you identify a resource block and specify the identifier for a resource pack. For example, the following line of code allocates the resource block named "IMAGES" to the pack with identifier “0”:
rh.allocateResourceBlockToPack ("IMAGES", 0);
At build time when Mobile Designer runs the resource handlers, it creates an individual file for each resource pack that your resource handler defines. When you use resource bundles, your resource handler should define, at a minimum, at least one resource pack that contains all the resource bundles. If a block is not included in a pack, Mobile Designer saves that block’s resources as individual resources in the final binary.
The resource packs you define in your resource handler are transparent to your application code. When Mobile Designer runs the resource handler, it keeps a record of each resource pack along with the blocks that the pack contains. It also keeps track of the resources that are in each block. When your application code loads a resource block into memory, Mobile Designer determines the appropriate pack to load for that block. As a result, you can customize the packs for each device your application supports without being concerned about altering your application code.