Creating Live Folders
To enable in-application live folder creation within your application, you need n An application with data exposed as a content provider An application that acts as a content type handler for the type of data that is exposed in the live folder (for example, VIEW field notes) To implement an Activity class to handle live folder creation To update the application’s content provider interface to handle live folder queries To configure the application’s Android manifest file for live folders Now let’s look at some of these requirements in more detail.An application that supports live folders must include a live folder creation activity.This activity is launched anytime the application reacts to the intent action ACTION_CREATE_LIVE_FOLDER.The live folder creation activity is responsible for one thing: responding with a specific instance of a live folder configuration. If you recall how the startActivityForResult() method works, this is exactly how the live folder creation activity is called.The activity needs to retrieve the incoming intent that performed the live folder creation request, craft a new live folder (as an Intent object with suitable extras to specify the live folder configuration details) and set the Activity result using the setResult() method.The setResult() method parameters can be used to communicate whether or not the live folder creation was successful as the resultCode parameter and pass back the specific instance of the live folder as the accompanying result Intent data parameter.
As you can see, the SimpleLiveFolderCreateActivity has a very short lifespan. It waits for ACTION_CREATE_LIVE_FOLDER requests and then crafts the appropriate Intent object to return as part of the activity result.The most important code in this activity is the code that creates the new intent called resultIntent.This Intent object contains all the configuration details for the new live folder instance.The setData() method is used to supply the live Uri (the Uri to query to fill the folder with data). Several extras are set to provide the live folder instance with a label and icon, as well as specify the display mode of the live folder. Live folders have several canned display modes:The DISPLAY_MODE_LIST value causes all live folder content to display in ListView control (ideal for text content) and the DISPLAY_MODEGRID displays live folder content in a GridView control—more appropriate if the live folder contents are graphics. Finally, the base Intent object for each live folder item is set. In this case, the base intent has an action type of VIEW, as you might expect, and therefore is compatible with the content type handler technique. For more information on the configuration details that can be applied to a live folder, see the Android SDK documentation for the android.provider.LiveFolders package.
Handling Live Folder Content Provider Queries
Each time the user opens the live folder, the system performs a content provider query. Therefore, the application’s content provider interface needs to be updated to handle queries to fill the live folder with data.As with search suggestions, you need to define a projection in order to map the content provider data columns to those that the live folder expects to use to fill the list or grid (depending on the display mode) within the folder.Whenever the live folder is opened by the user, the system executes a query on the Uri provided as part of the live folder configuration. Don’t forget to define the live Uri address and register it in the content provider’s UriMatcher object (using the addURI() method). For example, the field notes application used the Uri: content:// com.androidbook.simplelivefolder. SimpleFieldnotesContentProvider/fieldnotes/live By providing a special live folder Uri for the content provider queries, you can simply update the content provider’s query method to handle the specialized query, including building the projection, performing the appropriate query, and returning the results for display in the live folder.
Configuring the Android Manifest File for Live Folders
This query() method implementation handles both regular content queries and special live folder queries (those that come in with the live Uri).When the live folder query occurs,we simply use the handy setProjectionMap() method of the QueryBuilder object to set and execute the query as normal. Configuring the Android Manifest File for Live Folders Finally, the live folder Activity class needs to be registered within the application Android manifest file with an intent filter with the CREATE_LIVE_FOLDER action.The icon and text label shown in the Live Folder picker is set separately from the icon and label shown for a given instance of a live folder. You can set the information for the Live Folder in the picker using the android:label and android:icon attributes of the <activity> tag corresponding to the activity that handles the intent filter for android.intent.action.CREATE_LIVE_FOLDER action in the Android manifest file. The icon and label for each Live Folder instance on the Home screen (or other live folder host) is set as part intentextra LiveFolders.ACTION_CREATE_LIVE_FOLDER.