2011年5月16日月曜日

AndroidStudyMemo=Working with Live Folders

A LiveFolder (android.provider.LiveFolders) is a powerful feature that complements

the content provider interface.A LiveFolder is a special folder containing content

generated by a content provider. For example, a user might want to create a LiveFolder

with favorite contacts ("Fave Five"), most frequently viewed emails in a custom email application,

or high-priority tasks in a task management application.

When the user chooses to create a LiveFolder, the Android system provides a list of

all activities that respond to the ACTION_CREATE_LIVE_FOLDER Intent. If the user

chooses your Activity, that Activity creates the LiveFolder and passes it back to the

system using the setResult() method.

The LiveFolder consists of the following components:

Folder name

Folder icon

Display mode (grid or list)

Content provider URI for the folder contents

 

The first task when enabling a content provider to serve up data to a LiveFolder is to

provide an <intent-filter> for an Activity that handles enabling the LiveFolder.

This is done within the AndroidManifest.xml file as follows:

<intent-filter>

<action android:name=

"android.intent.action.CREATE_LIVE_FOLDER" />

<category

android:name="android.intent.category.DEFAULT" />

</intent-filter>

 

Next, this action needs to be handled within the onCreate() method of the Activity it

has been defined for.Within the preceding provider example, you can place the following

code to handle this action:

 

super.onCreate(savedInstanceState);

final Intent intent = getIntent();

final String action = intent.getAction();

if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {

final Intent resultIntent = new Intent();

resultIntent.setData(TrackPointProvider.LIVE_URI);

resultIntent.putExtra(

LiveFolders.EXTRA_LIVE_FOLDER_NAME, "GPX Sample");

resultIntent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,

Intent.ShortcutIconResource.fromContext(

this, R.drawable.icon));

resultIntent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,

LiveFolders.DISPLAY_MODE_LIST);

setResult(RESULT_OK, resultIntent);

} // ... rest of onCreate()

 

This defines the core components of the LiveFolder: its name, icon, display mode, and

Uri.The Uri is not the same as one that already existed because it needs certain specific

fields to work properly.This leads directly to the next task: modifying the content

provider to prepare it for serving up data to the LiveFolder.

First, you define a new Uri. In this case, you add "/live" to the end of the existing

CONTENT_URI. For example:

public static final Uri LIVE_URI = Uri.parse("content://"

+ AUTHORITY + "/" + TrackPointDatabase.TRACKPOINTS_TABLE

+ "/live");

 

 

You add this new Uri pattern the UriMatcher. Next, modify the query() implementation

to recognize this new Uri and add a projection, which is defined next:

 

switch (sURIMatcher.match(uri)) {

case TRACKPOINT_ID:

qBuilder.appendWhere("_id=" + uri.getLastPathSegment());

break;

case TRACKPOINTS_LIVE:

qBuilder.setProjectionMap(

TRACKPOINTS_LIVE_FOLDER_PROJECTION_MAP);

break;

// ... other cases

}

Cursor c = qBuilder.query( // ...

 

The projection is critical for a working LiveFolder provider.There are two mandatory

fields that must be in the resulting Cursor: LiveFolder._ID and LiveFolder.NAME. In

addition to these, other fields, such as LiveFolder.DESCRIPTION, are available to modify

the look and behavior of the view. In this example,we use TIMESTAMP for the name, as

shown here in the following projection implementation:

 

private static final HashMap<String,String>

TRACKPOINTS_LIVE_FOLDER_PROJECTION_MAP;

static {

TRACKPOINTS_LIVE_FOLDER_PROJECTION_MAP =

new HashMap<String,String>();

TRACKPOINTS_LIVE_FOLDER_PROJECTION_MAP.put(

LiveFolders._ID, _ID + " as " + LiveFolders._ID);

TRACKPOINTS_LIVE_FOLDER_PROJECTION_MAP.put(

LiveFolders.NAME, TIMESTAMP + " as " + LiveFolders.NAME);

}

 

After this is done, the LiveFolder should be,well, live. In this example, only a list of dates

is shown.

 

Summary

Your application can leverage the data available within other Android applications, if they

expose that data as a content provider. Content providers such as MediaStore, Browser,

CallLog, and Contacts can be leveraged by other Android applications, resulting in a robust,

immersive experience for users. Applications can also share data among themselves

by becoming content providers. Becoming a content provider involves implementing a

set of methods that manage how and what data you expose for use in other applications

or even directly on the Home screen through the use of LiveFolders.

 

 

0 件のコメント:

コメントを投稿